1. 程式人生 > >AlertDialog和AlertDialog.Builder兩者區別

AlertDialog和AlertDialog.Builder兩者區別

 那我們先了解什麼是AlertDialog?什麼是AlertDialog.Builder?且兩者有什麼區別?

   AlertDialog是Dialog的一個直接子類,AlertDialog也是Android系統當中最常用的對話方塊之一。
   一個AlertDialog可以有兩個以上的Button,可以對一個AlertDialog設定相應的資訊。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。

   但不能直接通過AlertDialog的建構函式來生產一個AlertDialog。研究AlertDialog的原始碼發現AlertDialog所有的構造方法都是防寫的所以不能通過:AlertDialog alertDialog  = new AlertDialog();來得到。AlertDialog構造方法原始碼如下:

  1. protected AlertDialog(Context context) {     
  2.     this(context, com.android.internal.R.style.Theme_Dialog_Alert);     
  3. }     
  4. protected AlertDialog(Context context, int theme) {     
  5.     super(context, theme);     
  6.     mAlert = new AlertController(context, this, getWindow());     
  7. }     
  8. protected AlertDialog(Context context, 
    boolean cancelable, OnCancelListener cancelListener) {     
  9.     super(context, com.android.internal.R.style.Theme_Dialog_Alert);     
  10.     setCancelable(cancelable);     
  11.     setOnCancelListener(cancelListener);     
  12.     mAlert = new AlertController(context, this, getWindow());     
  13. }    


只能通過:

  1. AlertDialog.Builder alertDialog  =
    new  AlertDialog.Builder(this);    

來得到。

那就通過一個具體的例項來說說吧(這裡用一個最常用的對話方塊為例):

  1. package com.oyah;     
  2. import android.app.Activity;     
  3. import android.app.AlertDialog;     
  4. import android.content.DialogInterface;     
  5. import android.os.Bundle;     
  6. publicclass TestsActivity extends Activity {     
  7.     publicvoid onCreate(Bundle savedInstanceState) {     
  8.         super.onCreate(savedInstanceState);     
  9.         setContentView(R.layout.main);     
  10.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);     
  11.         alertDialog     
  12.                 .setTitle(”title”)     
  13.                 .setMessage(”message”)     
  14.                 .setPositiveButton(”okBuuon”,     
  15.                         new DialogInterface.OnClickListener() {     
  16.                             @Override
  17.                             publicvoid onClick(DialogInterface dialog,     
  18.                                     int which) {     
  19.                             }     
  20.                         })     
  21.                 .setNegativeButton(”exitButton”,     
  22.                         new DialogInterface.OnClickListener() {     
  23.                             @Override
  24.                             publicvoid onClick(DialogInterface dialog,     
  25.                                     int which) {     
  26.                             }     
  27.                         }).[b]setCancelable(false).[/b]create().show();     
  28.     }     
  29. }    


針對AlertDialog中設定了確定和取消按鈕,一般來說確定為執行某個動作,取消就是不執行,但是如果使用者點選了系統的Back鍵,此時就會將AlertDialog關閉,而並沒有執行預期的取消的操作。

此時需要關注一個方法setCancelable(false) 該方法定義設定該AlertDialog是否可以被Back鍵取消,如果不設定預設為true
下面是一些擴充套件:
根據AlertDialog.Builder 建立 相應的 AlertDialog

  1. AlertDialog alertDialogs = alertDialog.create();    


用dismiss();方法來清除建立過的AlertDialog

  1. alertDialogs.dismiss();    


以上所採用的都是AlertDialog 系統預設的佈局 現在說自定義佈局的情況 並新增一個用於取消AlertDialog 的 Button

定義其佈局 main.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:orientation=”horizontal”
  4.     android:layout_width=”fill_parent”
  5.     android:layout_height=”fill_parent”
  6.     android:padding=”10dp”
  7.     >  
  8. <LinearLayout   
  9.     android:orientation=”vertical”
  10.     android:layout_width=”wrap_content”
  11.     android:layout_height=”wrap_content”
  12.     >  
  13.  </LinearLayout>  
  14. </LinearLayout>  


通過LayoutInflater 得到上面 main.xml 佈局檔案

  1. view = this.getLayoutInflater().inflate(R.layout.main, null);  


指定AlertDialog.Builder 所需的佈局 並返回目標AlertDialog

  1. alertDialog.setView(view);  
  2. alertDialogs= alertDialog.create();  


通過 view.findViewById() 得到 目標View 然後設定其內容 如:

  1. TextView title = (TextView) view.findViewById(R.id.title);  
  2. title.setTextSize(20);  
  3. title.setTextColor(Color.RED);  
  4. title.setText(”Title”);  
  5. TextView message = (TextView) view.findViewById(R.id.message);  
  6. message.setText(”message”);  


顯示對話方塊 AlertDialog

  1. findViewById(R.id.button).setOnClickListener(new OnClickListener(){  
  2.             publicvoid onClick(View v) {  
  3.                 // TODO Auto-generated method stub
  4.                 alertDialogs.show();  
  5.             }  
  6.         });  


清除對話方塊 AlertDialog

  1. view.setOnClickListener(new OnClickListener(){  
  2.             publicvoid onClick(View v) {  
  3.                 // TODO Auto-generated method stub
  4.                 alertDialogs.dismiss();  
  5.             }  
  6.         });  
                            </td>
                            <script>
                                var html = document.getElementById("artContent").innerHTML;
                                document.getElementById("artContent").innerHTML = html;

                            </script>
                        </tr>
                    </tbody>
                </table>
                <div id="viewerPlaceHolder" style="width: 717px; height: 700px; display: none; margin: 0 auto;">
                </div>