如何在Android中自定義Dialog

本篇文章為大家展示了如何在Android中自定義Dialog,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

廣靈網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,廣靈網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為廣靈上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的廣靈做網(wǎng)站的公司定做!

功能:

android 提供給我們的只有2種Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特點:1. 不是 Activity 2. 開銷比 Activity 小得多

原理:

1. android 系統(tǒng)提供了一個class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先調(diào)用系統(tǒng)的"super.onCreate(savedInstanceState)" 其就會保證調(diào)用這個method.

2. 至于 Dialog 界面的定制 可以寫一個xml 文件 然后 在 "void onCreate(Bundle)" 通過 "setContentView()" 來使之生效

3. Dialog 使用問題: 1. 彈出:show() 2. 取消:dismiss()

代碼:

1. 創(chuàng)建一個 Dialog:

public class CustomDialog extends Dialog {
  public CustomDialog(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_dialog);
    setTitle("Custom Dialog");
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageResource(R.drawable.sepurple);
    Button buttonYes = (Button) findViewById(R.id.button_yes);
    buttonYes.setHeight(5);
    buttonYes.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
    Button buttonNo = (Button) findViewById(R.id.button_no);
    buttonNo.setSingleLine(true);
    buttonNo.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
  }
  //called when this dialog is dismissed
  protected void onStop() {
    Log.d("TAG","+++++++++++++++++++++++++++");
  }
}

2. Dialog 的使用:

public class CustomDialogUsage extends Activity {
  CustomDialog cd;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cd = new CustomDialog(this);
    Button buttonYes = (Button) findViewById(R.id.main_button);
    buttonYes.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        cd.show();
      }
    });
  }
}

3. Dialog 的界面定制:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
  <ImageView android:id="@+id/image" android:layout_width="wrap_content"
  android:layout_height="fill_parent" android:layout_marginRight="10dp"
  />
  <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:padding="5px">
    <TextView android:id="@+id/text" android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF" />
    <LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px">
      <Button android:id="@+id/button_yes"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=" Yes " android:gravity="center"
      />
      <Button android:id="@+id/button_no"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=" No " android:gravity="center"
      />
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

上述內(nèi)容就是如何在Android中自定義Dialog,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁題目:如何在Android中自定義Dialog
鏈接URL:http://bm7419.com/article2/ggoooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)域名注冊網(wǎng)站排名、服務(wù)器托管、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)