Refer:http://www.2cto.com/kf/201205/131876.html
(一)最簡單的用法(詳見注釋)
1 // 1、創建簡單的AlertDialog // AlertDialog的構造方法全部是Protected的, 2 // 所以不能直接通過new一個AlertDialog來創建出一個AlertDialog; // 3 // (1)要創建一個AlertDialog,就要用到AlertDialog.Builder 4 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 5 6 // (2)設置各種屬性 // 注:不設置哪項屬性,這個屬性就默認不會顯示出來 7 dialog.setTitle("這是一個簡單的對話框"); 8 dialog.setIcon(R.drawable.dictation2_64); 9 dialog.setMessage("歡迎使用!"); 10 11 // (3)設置dialog可否被取消 12 dialog.setCancelable(true); 13 14 // (4)顯示出來 15 dialog.show();
效果如下:
(二)帶按鈕的AlertDialog
1 // 2、帶按鈕的AlertDialog 2 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 3 dialog.setTitle("確認"); 4 dialog.setIcon(R.drawable.dictation2_64); 5 dialog.setMessage("確定要刪除此項嗎?"); 6 7 // 設置“確定”按鈕,使用DialogInterface.OnClickListener接口參數 8 dialog.setPositiveButton("確定", 9 new DialogInterface.OnClickListener() { 10 11 @Override 12 public void onClick(DialogInterface dialog, int which) { 13 Log.d("Dialog", "點擊了“確認”按鈕"); 14 } 15 }); 16 17 // 設置“查看詳情”按鈕,使用DialogInterface.OnClickListener接口參數 18 dialog.setNeutralButton("查看詳情", 19 new DialogInterface.OnClickListener() { 20 21 @Override 22 public void onClick(DialogInterface dialog, int which) { 23 Log.d("Dialog", "點擊了“查看詳情”按鈕"); 24 } 25 }); 26 27 // 設置“取消”按鈕,使用DialogInterface.OnClickListener接口參數 28 dialog.setNegativeButton("取消", 29 new DialogInterface.OnClickListener() { 30 31 @Override 32 public void onClick(DialogInterface dialog, int which) { 33 Log.d("Dialog", "點擊了“取消”按鈕"); 34 } 35 }); 36 37 dialog.show();
效果如下:
(三)類似於ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法來實現類似ListView的AlertDialog。
1 // 3、類似ListView的AlertDialog 2 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 3 dialog.setTitle("請選擇一項運動"); 4 dialog.setIcon(R.drawable.dictation2_64); 5 // 設置為false說明當前dialog是不能用返回鍵取消的 6 dialog.setCancelable(false); 7 8 // 列表字符串數組 9 final String[] sportsArray = new String[] { "跑步", "籃球", "游泳", 10 "自行車", "羽毛球" }; 11 // 用於在item的點擊事件中,記錄選擇的是哪一項,初始值設為0.這里用final數組只是因為匿名內部類中只能使用外部終態的變量 12 final int selectedIndex[] = { 0 }; 13 14 // 用setItems方法來實現 15 dialog.setItems(sportsArray, new DialogInterface.OnClickListener() { 16 17 @Override 18 public void onClick(DialogInterface dialog, int which) { 19 selectedIndex[0] = which; 20 Log.d("Dialog", "選擇了:" + sportsArray[selectedIndex[0]]); 21 } 22 }); 23 24 dialog.setNegativeButton("取消", 25 new DialogInterface.OnClickListener() { 26 27 @Override 28 public void onClick(DialogInterface dialog, int which) { 29 Log.d("Dialog", "點擊了“取消”按鈕"); 30 } 31 }); 32 33 dialog.show();
效果如下:
(四)類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法來實現類似RadioButton的AlertDialog
第一個參數是要顯示的數據的數組,第二個參數是初始值(初始被選中的item),第三個參數是點擊某個item的觸發事件
1 // 4、類似RadioButton的AlertDialog 2 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 3 dialog.setTitle("請選擇一項運動"); 4 dialog.setIcon(R.drawable.dictation2_64); 5 6 // 列表字符串數組 7 final String[] sportsArray = new String[] { "跑步", "籃球", "游泳", 8 "自行車", "羽毛球" }; 9 // 用於在item的點擊事件中,記錄選擇的是哪一項,初始值設為0.這里用final數組只是因為匿名內部類中只能使用外部終態的變量 10 final int selectedIndex[] = { 0 }; 11 12 // 用setSingleChoiceItems方法來實現 13 dialog.setSingleChoiceItems(sportsArray, 0, 14 new DialogInterface.OnClickListener() { 15 16 @Override 17 public void onClick(DialogInterface dialog, int which) { 18 selectedIndex[0] = which; 19 20 } 21 }); 22 23 dialog.setPositiveButton("確定", 24 new DialogInterface.OnClickListener() { 25 26 @Override 27 public void onClick(DialogInterface dialog, int which) { 28 Log.d("Dialog", "選擇了:" 29 + sportsArray[selectedIndex[0]]); 30 } 31 }); 32 33 dialog.setNegativeButton("取消", 34 new DialogInterface.OnClickListener() { 35 36 @Override 37 public void onClick(DialogInterface dialog, int which) { 38 Log.d("Dialog", "點擊了“取消”按鈕"); 39 } 40 }); 41 42 dialog.show();
效果如下:
(五)類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法來實現類似CheckBox的AlertDialog
第一個參數是要顯示的數據的數組,第二個參數是選中狀態的數組,第三個參數是點擊某個item的觸發事件
1 // 5、類似CheckBox的AlertDialog 2 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 3 dialog.setTitle("請選擇喜歡的運動(可多選)"); 4 dialog.setIcon(R.drawable.dictation2_64); 5 6 // 列表字符串數組 7 final String[] sportsArray = new String[] { "跑步", "籃球", "游泳", 8 "自行車", "羽毛球" }; 9 // 用於在item的點擊事件中,記錄選擇了哪些項. 10 final boolean[] selectedIndex = { true, true, false, false, false }; 11 12 // 用setMultiChoiceItems方法來實現 13 dialog.setMultiChoiceItems(sportsArray, selectedIndex, 14 new DialogInterface.OnMultiChoiceClickListener() { 15 16 @Override 17 public void onClick(DialogInterface dialog, int which, 18 boolean isChecked) { 19 selectedIndex[which] = isChecked; 20 } 21 }); 22 23 dialog.setPositiveButton("確定", 24 new DialogInterface.OnClickListener() { 25 26 @Override 27 public void onClick(DialogInterface dialog, int which) { 28 for (int i = 0; i < selectedIndex.length; i++) { 29 if (selectedIndex[i]) { 30 Log.d("Dialog", "選擇了:" + sportsArray[i]); 31 } 32 } 33 } 34 }); 35 36 dialog.setNegativeButton("取消", 37 new DialogInterface.OnClickListener() { 38 39 @Override 40 public void onClick(DialogInterface dialog, int which) { 41 Log.d("Dialog", "點擊了“取消”按鈕"); 42 } 43 }); 44 45 dialog.show();
效果如下:
(六)自定義View的AlertDialog
有時候系統自帶的AlertDialog風格不能滿足我們的需求,就比如說我們要實現一個Login畫面,有用戶名和密碼,這時我們就要用到自定義View的AlertDialog
1、先創建自定義登錄框的布局文件my_login_view.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:gravity="center" 11 android:padding="5dp" > 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="賬號:" /> 17 18 <EditText 19 android:id="@+id/my_login_account_et" 20 android:layout_width="0dip" 21 android:layout_height="wrap_content" 22 android:layout_weight="1" /> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:gravity="center" 29 android:padding="5dp" > 30 31 <TextView 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="密碼:" /> 35 36 <EditText 37 android:id="@+id/my_login_password_et" 38 android:layout_width="0dip" 39 android:layout_height="wrap_content" 40 android:layout_weight="1" 41 android:inputType="numberPassword" /> 42 </LinearLayout> 43 44 </LinearLayout>
2、在Activity的合適地方創建自定義的AlertDialog(比如按鈕的點擊事件中):
1 // 6、自定義View的AlertDialog 2 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 3 dialog.setTitle("用戶登錄"); 4 5 // 取得自定義View 6 LayoutInflater layoutInflater = LayoutInflater.from(this); 7 final View myLoginView = layoutInflater.inflate( 8 R.layout.my_login_view, null); 9 dialog.setView(myLoginView); 10 11 dialog.setPositiveButton("確定", 12 new DialogInterface.OnClickListener() { 13 14 @Override 15 public void onClick(DialogInterface dialog, int which) { 16 EditText loginAccountEt = (EditText) myLoginView 17 .findViewById(R.id.my_login_account_et); 18 EditText loginPasswordEt = (EditText) myLoginView 19 .findViewById(R.id.my_login_password_et); 20 Log.d("MyLogin Dialog", "輸入的用戶名是:" 21 + loginAccountEt.getText().toString()); 22 Log.d("MyLogin Dialog", "輸入的密碼是:" 23 + loginPasswordEt.getText().toString()); 24 } 25 }); 26 27 dialog.setNegativeButton("取消", 28 new DialogInterface.OnClickListener() { 29 30 @Override 31 public void onClick(DialogInterface dialog, int which) { 32 33 } 34 }); 35 36 dialog.show();
效果如下:
點擊“確定”按鈕后LogCat中的內容: