天天看點

Android中的對話框AlertDialog使用技巧合集

  今天我用自己寫的一個Demo 和大家詳細介紹一個Android中的對話框的使用技巧。 

Android中的對話框AlertDialog使用技巧合集

1.确定取消對話框

對話框中有2個按鈕   通過調用 setPositiveButton 方法 和 setNegativeButton 方法 可以設定按鈕的顯示内容以及按鈕的監聽事件。

Android中的對話框AlertDialog使用技巧合集

我們使用AlerDialog 建立對話框

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   

使用builder設定對話框的title button icon 等等

view plain

  1. builder.setIcon(R.drawable.icon);  
  2.        builder.setTitle("你确定要離開嗎?");  
  3.        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  4.            public void onClick(DialogInterface dialog, int whichButton) {  
  5.                //這裡添加點選确定後的邏輯  
  6.                showDialog("你選擇了确定");  
  7.            }  
  8.        });  
  9.        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  10.            public void onClick(DialogInterface dialog, int whichButton) {  
  11.                //這裡添加點選确定後的邏輯  
  12.                showDialog("你選擇了取消");  
  13.            }  
  14.        });  
  15.        builder.create().show();  

   這個dialog用于現實onClick後監聽的内容資訊

view plain

  1. private void showDialog(String str) {  
  2. w AlertDialog.Builder(MainDialog.this)  
  3.      .setMessage(str)  
  4.      .show();  
  5. }  

2.多個按鈕資訊框

Android中的對話框AlertDialog使用技巧合集
Android中的對話框AlertDialog使用技巧合集

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
  2. builder.setIcon(R.drawable.icon);  
  3. builder.setTitle("投票");  
  4. builder.setMessage("您認為什麼樣的内容能吸引您?");  
  5. builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {  
  6.     public void onClick(DialogInterface dialog, int whichButton) {  
  7.         showDialog("你選擇了有趣味的");  
  8.     }  
  9. });  
  10. builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {  
  11.     public void onClick(DialogInterface dialog, int whichButton) {  
  12.         showDialog("你選擇了有思想的");                      
  13.     }  
  14. });  
  15. builder.setNegativeButton("主題強的", new DialogInterface.OnClickListener() {  
  16.     public void onClick(DialogInterface dialog, int whichButton) {  
  17.         showDialog("你選擇了主題強的");    
  18.     }  
  19. });  
  20. builder.create().show();  

3.清單框

Android中的對話框AlertDialog使用技巧合集

這個數組用于清單選擇

view plain

  1. final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};  

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.         builder.setTitle("清單選擇框");  
  3.         builder.setItems(mItems, new DialogInterface.OnClickListener() {  
  4.             public void onClick(DialogInterface dialog, int which) {  
  5.                 //點選後彈出視窗選擇了第幾項  
  6.                 showDialog("你選擇的id為" + which + " , " + mItems[which]);  
  7.             }  
  8.         });  
  9.         builder.create().show();  

4.單項選擇清單框

Android中的對話框AlertDialog使用技巧合集
Android中的對話框AlertDialog使用技巧合集

mSingleChoice 用于記錄單選中的ID

view plain

  1. int mSingleChoiceID = -1;  

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2. mSingleChoiceID = -1;  
  3. builder.setIcon(R.drawable.icon);  
  4.     builder.setTitle("單項選擇");  
  5.     builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {  
  6.         public void onClick(DialogInterface dialog, int whichButton) {  
  7.                 mSingleChoiceID = whichButton;  
  8.                 showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);  
  9.         }  
  10.     });  
  11.     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  12.         public void onClick(DialogInterface dialog, int whichButton) {  
  13.             if(mSingleChoiceID > 0) {  
  14.             showDialog("你選擇的是" + mSingleChoiceID);  
  15.             }  
  16.         }  
  17.     });  
  18.     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  19.         public void onClick(DialogInterface dialog, int whichButton) {  
  20.         }  
  21.     });  
  22.    builder.create().show();  

5.進度條框

Android中的對話框AlertDialog使用技巧合集

點選進度條框按鈕後 開啟一個線程計算讀取的進度 假設讀取結束為 100

Progress在小于100的時候一直線上程中做循環++ 隻到讀取結束後,停止線程。

view plain

  1.           mProgressDialog = new ProgressDialog(MainDialog.this);  
  2.      mProgressDialog.setIcon(R.drawable.icon);  
  3.      mProgressDialog.setTitle("進度條視窗");  
  4.      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  5.      mProgressDialog.setMax(MAX_PROGRESS);  
  6.      mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() {  
  7.          public void onClick(DialogInterface dialog, int whichButton) {  
  8.              //這裡添加點選後的邏輯  
  9.          }  
  10.      });  
  11.      mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {  
  12.          public void onClick(DialogInterface dialog, int whichButton) {  
  13.              //這裡添加點選後的邏輯  
  14.          }  
  15.      });  
  16.      mProgressDialog.show();  
  17.      new Thread(this).start();  
  18. ic void run() {  
  19. int Progress = 0;  
  20. while(Progress < MAX_PROGRESS) {  
  21. try {  
  22.     Thread.sleep(100);  
  23.     Progress++;    
  24.     mProgressDialog.incrementProgressBy(1);  
  25. } catch (InterruptedException e) {  
  26.     // TODO Auto-generated catch block  
  27.     e.printStackTrace();  
  28. }  
  29. }  

6.多項選擇清單框

Android中的對話框AlertDialog使用技巧合集
Android中的對話框AlertDialog使用技巧合集

MultiChoiceID 用于記錄多選選中的id号 存在ArrayList中

選中後 add 進ArrayList

取消選中後 remove 出ArrayList 。

view plain

  1. ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();  

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2. MultiChoiceID.clear();  
  3. builder.setIcon(R.drawable.icon);  
  4.     builder.setTitle("多項選擇");  
  5.     builder.setMultiChoiceItems(mItems,  
  6.             new boolean[]{false, false, false, false, false, false, false},  
  7.             new DialogInterface.OnMultiChoiceClickListener() {  
  8.                 public void onClick(DialogInterface dialog, int whichButton,  
  9.                         boolean isChecked) {  
  10.                    if(isChecked) {  
  11.                        MultiChoiceID.add(whichButton);  
  12.                        showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);  
  13.                    }else {  
  14.                        MultiChoiceID.remove(whichButton);  
  15.                    }  
  16.                 }  
  17.             });  
  18.     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  19.         public void onClick(DialogInterface dialog, int whichButton) {  
  20.             String str = "";  
  21.             int size = MultiChoiceID.size();  
  22.             for (int i = 0 ;i < size; i++) {  
  23.             str+= mItems[MultiChoiceID.get(i)] + ", ";  
  24.             }  
  25.             showDialog("你選擇的是" + str);  
  26.         }  
  27.     });  
  28.     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  29.         public void onClick(DialogInterface dialog, int whichButton) {  
  30.         }  
  31.     });  
  32.    builder.create().show();  

7.自定義布局

Android中的對話框AlertDialog使用技巧合集

講到自定義布局我就得多說一說了,為什麼要多說一說呢? 

其實自定義布局在Android的開發中非常重要 因為它能讓開發者做出自己五彩缤紛的Activity 而不用去使用系統枯燥的界面。

自定義dialog有什麼好處?

比如我們在開發過長當中 要通過介紹系統發送的一個廣播彈出一個dialog . 但是dialog必需是基于activity才能呈現出來 如果沒有activity 的話 程式就會崩潰。是以我們可以寫一個自定義的 dialog 把它定義成一個activity

這樣我們收到一條打開dialog的廣播後 直接啟動這個 activity  程式正常運作~~ 

這就是自定義dialog的好處。

注明:下面這個例子隻是寫了自定義dialog 沒有把它單獨的寫在一個activity中 如果須要的話 可以自己改一下。

view plain

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.  LayoutInflater factory = LayoutInflater.from(this);  
  3.  final View textEntryView = factory.inflate(R.layout.test, null);  
  4.      builder.setIcon(R.drawable.icon);  
  5.      builder.setTitle("自定義輸入框");  
  6.      builder.setView(textEntryView);  
  7.      builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  8.          public void onClick(DialogInterface dialog, int whichButton) {  
  9.          EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);  
  10.          EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);  
  11.          showDialog("姓名 :"  + userName.getText().toString()  + "密碼:" + password.getText().toString() );  
  12.          }  
  13.      });  
  14.      builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  15.          public void onClick(DialogInterface dialog, int whichButton) {  
  16.          }  
  17.      });  
  18.    builder.create().show();  

view plain

  1. <span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_height="wrap_content"   
  4. android:layout_width="wrap_content"  
  5. android:orientation="horizontal"  
  6. android:id="@+id/dialog">  
  7. <LinearLayout  
  8. android:layout_height="wrap_content"   
  9. android:layout_width="wrap_content"  
  10. android:orientation="horizontal"  
  11. android:id="@+id/dialogname">  
  12. <TextView android:layout_height="wrap_content"  
  13.    android:layout_width="wrap_content"  
  14.   android:id="@+id/tvUserName"   
  15.   android:text="姓名:" />  
  16. <EditText android:layout_height="wrap_content"  
  17.   android:layout_width="wrap_content"   
  18.   android:id="@+id/etUserName"   
  19.   android:minWidth="200dip"/>  
  20. </LinearLayout>    
  21. <LinearLayout  
  22. android:layout_height="wrap_content"   
  23. android:layout_width="wrap_content"  
  24. android:orientation="horizontal"  
  25. android:id="@+id/dialognum"  
  26.  android:layout_below="@+id/dialogname"  
  27. >  
  28.   <TextView android:layout_height="wrap_content"  
  29.    android:layout_width="wrap_content"  
  30.   android:id="@+id/tvPassWord"   
  31.   android:text="密碼:" />  
  32. <EditText android:layout_height="wrap_content"  
  33.   android:layout_width="wrap_content"   
  34.   android:id="@+id/etPassWord"   
  35.   android:minWidth="200dip"/>  
  36.  </LinearLayout>    
  37.   </RelativeLayout></span>  

8.讀取進度框

顯示一個正在轉圈的進度條loading

view plain

  1. mProgressDialog = new ProgressDialog(this);  
  2.  mProgressDialog.setTitle("讀取ing");  
  3.  mProgressDialog.setMessage("正在讀取中請稍候");  
  4.  mProgressDialog.setIndeterminate(true);  
  5.  mProgressDialog.setCancelable(true);  
  6.  mProgressDialog.show();