Android軟件開發之盤點所有Dialog對話框大合集(一)

                                                                                                      對話框大合集


雨鬆MOMO原創文章如轉載,請註明:轉載自雨鬆MOMO的博客原文地址:http://blog.csdn.net/xys289187120/article/details/6601613





1.確定取消對話框


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



我們使用AlerDialog 創建對話框

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

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

  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後監聽的內容信息

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



2.多個按鈕信息框




  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.列表框




這個數組用於列表選擇

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




  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.單項選擇列表框



mSingleChoice 用於記錄單選中的ID

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




5.進度條框



點擊進度條框按鈕後 開啓一個線程計算讀取的進度 假設讀取結束爲 100
Progress在小於100的時候一直在線程中做循環++ 只到讀取結束後,停止線程。
  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.  
  19. ic void run() { 
  20. int Progress = 0
  21. while(Progress < MAX_PROGRESS) { 
  22. try
  23.     Thread.sleep(100); 
  24.     Progress++;   
  25.     mProgressDialog.incrementProgressBy(1); 
  26. } catch (InterruptedException e) { 
  27.     // TODO Auto-generated catch block 
  28.     e.printStackTrace(); 
  29.   


6.多項選擇列表框




MultiChoiceID 用於記錄多選選中的id號 存在ArrayList中
選中後 add 進
ArrayList
取消選中後 remove 出ArrayList


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

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


7.自定義佈局



講到自定義佈局我就得多說一說了,爲什麼要多說一說呢?
其實自定義佈局在Android的開發中非常重要 因爲它能讓開發者做出自己五彩繽紛的Activity 而不用去使用系統枯燥的界面。

自定義dialog有什麼好處?

比如我們在開發過長當中 要通過介紹系統發送的一個廣播彈出一個dialog . 但是dialog必需是基於activity才能呈現出來 如果沒有activity 的話 程序就會崩潰。所以我們可以寫一個自定義的 dialog 把它定義成一個activity
這樣我們收到一條打開dialog的廣播後 直接啓動這個 activity  程序正常運行~~

這就是自定義dialog的好處。

註明:下面這個例子只是寫了自定義dialog 沒有把它單獨的寫在一個activity中 如果須要的話 可以自己改一下。
  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.           
  10.          EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName); 
  11.          EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord); 
  12.          showDialog("姓名 :"  + userName.getText().toString()  + "密碼:" + password.getText().toString() ); 
  13.          } 
  14.      }); 
  15.      builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
  16.          public void onClick(DialogInterface dialog, int whichButton) { 
  17.  
  18.          } 
  19.      }); 
  20.    builder.create().show(); 



  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.  
  13. <TextView android:layout_height="wrap_content" 
  14.    android:layout_width="wrap_content" 
  15.   android:id="@+id/tvUserName"  
  16.   android:text="姓名:" /> 
  17. <EditText android:layout_height="wrap_content" 
  18.   android:layout_width="wrap_content"  
  19.   android:id="@+id/etUserName"  
  20.   android:minWidth="200dip"/> 
  21. </LinearLayout>   
  22. <LinearLayout 
  23. android:layout_height="wrap_content"  
  24. android:layout_width="wrap_content" 
  25. android:orientation="horizontal" 
  26. android:id="@+id/dialognum" 
  27. android:layout_below="@+id/dialogname" 
  28. > 
  29.   <TextView android:layout_height="wrap_content" 
  30.    android:layout_width="wrap_content" 
  31.   android:id="@+id/tvPassWord"  
  32.   android:text="密碼:" /> 
  33. <EditText android:layout_height="wrap_content" 
  34.   android:layout_width="wrap_content"  
  35.   android:id="@+id/etPassWord"  
  36.   android:minWidth="200dip"/> 
  37. </LinearLayout>   
  38.   </RelativeLayout></span> 

8.讀取進度框

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

  1. mProgressDialog = new ProgressDialog(this); 
  2. mProgressDialog.setTitle("讀取ing"); 
  3. mProgressDialog.setMessage("正在讀取中請稍候"); 
  4. mProgressDialog.setIndeterminate(true); 
  5. mProgressDialog.setCancelable(true); 
  6. mProgressDialog.show(); 
發佈了48 篇原創文章 · 獲贊 34 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章