Android Dialog AlertDialog


1、普通的對話框

  

    

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:gravity="center"
 5     android:orientation="vertical" >
 6 
 7     <ProgressBar
 8         android:id="@+id/progressBar1"
 9         style="?android:attr/progressBarStyleLarge"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content" />
12 
13 </LinearLayout>
Dialog_progress.xml
 1 public class MyDialog extends Dialog{
 2 
 3     //必須要給構造方法
 4     public MyDialog(Context context) {
 5         //也可以在構建Dialog對象的時候就給指定Dialog樣式
 6         //使用主題來修改Dialog樣式,在res/values/styles.xml中添加自定義主題
 7         super(context,R.style.DialogTheme);
 8     }
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         //這個可以不要標題。通過getWindow().requestFeature(featureId)方法
14         //getWindow().requestFeature(Window.FEATURE_NO_TITLE);
15         setContentView(R.layout.dialog_progress);
16     }
17     
18     /**
19      * 一個Activity或者一個Dialog剛剛出現在用戶面前的時候,焦點改變調用onWindowFocusChanged
20      */
21     @Override
22     public void onWindowFocusChanged(boolean hasFocus) {
23         super.onWindowFocusChanged(hasFocus);
24     }
25 }
MyDialog
 1 // 普通對話框
 2     public void dialog1(View v) {
 3         MyDialog dialog = new MyDialog(this);
 4         dialog.setTitle("這是進度Dialog");
 5         // 顯示對話框
 6         dialog.show();
 7 
 8         // 關閉對話框用
 9         // dialog.dismiss();
10     }
普通對話框

 

2、警告對話框AlertDialog setMessage

 

 1 //AlertDialog  setMessage
 2     public void dialog2(View v) {
 3         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
 4         dialog.setTitle("警告!").setIcon(R.drawable.ic_launcher)
 5                 .setMessage("前方高能")
 6                 // 注意這個導的包是import android.content.DialogInterface.OnClickListener;
 7                 .setPositiveButton("OK", new OnClickListener() {
 8                     @Override
 9                     public void onClick(DialogInterface dialog, int which) {
10                         Toast.makeText(MainActivity.this, "您選擇了OK",
11                                 Toast.LENGTH_SHORT).show();
12 
13                     }
14                 })
15                 .setNegativeButton("Cancel", new OnClickListener() {
16                     @Override
17                     public void onClick(DialogInterface dialog, int which) {
18                         Toast.makeText(MainActivity.this, "您選擇了Cancel",
19                                 Toast.LENGTH_SHORT).show();
20                     }
21                 })
22                 .setNeutralButton("Ignore", new OnClickListener() {
23                     @Override
24                     public void onClick(DialogInterface dialog, int which) {
25                         Toast.makeText(MainActivity.this, "您選擇了Ignore",
26                                 Toast.LENGTH_SHORT).show();
27                     }
28                 })
29                 .create().show();
30     }
警告對話框,setMessage

 

 

3、菜單對話框AlertDialog setItem 

 1 //菜單選擇,  setItem 如果設置setMessage,那么只會顯示Message
 2     String[] setting = {"聲音","存儲","顯示","應用","語言和輸入法","流量使用情況","WLAN"};
 3     public void dialog3(View v){
 4         new AlertDialog.Builder(this)
 5         .setTitle("設置")
 6         .setIcon(R.drawable.setting)
 7         //which代表第幾項,item點擊后自動關閉,不需要Button
 8         .setItems(setting, new OnClickListener() {
 9             @Override
10             public void onClick(DialogInterface dialog, int which) {
11                 Toast.makeText(MainActivity.this, setting[which], Toast.LENGTH_SHORT).show();
12             }
13         })
14         .create().show();
15     }
菜單對話框 setItem

 

4、單選對話框AlertDialog setSingleChoiceItems

 1 //單選對話框,setItem
 2     String[] hobby = {"唱歌","跑步","武術","乒乓球","敲代碼"};
 3     int choice = 0;
 4     public void dialog4(View v){
 5         new AlertDialog.Builder(this)
 6         .setTitle("愛好單選")
 7         .setIcon(R.drawable.hobby)
 8         //0代表默認選中第一個,選中不會自動關閉
 9         .setSingleChoiceItems(hobby, 0, new OnClickListener() {
10             @Override
11             public void onClick(DialogInterface dialog, int which) {
12                 choice = which;
13             }
14         })
15         //Button上的which永遠為0,所以這里需要一個變量來保存選中的ItemID
16         .setPositiveButton("ok", new OnClickListener() {
17             @Override
18             public void onClick(DialogInterface dialog, int which) {
19                 Toast.makeText(MainActivity.this, hobby[choice], Toast.LENGTH_SHORT).show();
20             }
21         })
22         .setNegativeButton("cancel",null)
23         .create().show();
24     }
單選對話框 setSingleChoiceItems

 

5、多選對話框AlertDialog setMultiChoiceItem

 

 1 String[] hobby = {"唱歌","跑步","武術","乒乓球","敲代碼"};
 2     boolean[] bool = {false,false,false,false,false};
 3     List<String> list = new ArrayList<String>();
 4     public void dialog5(View v){
 5         new AlertDialog.Builder(this)
 6         .setTitle("愛好可多選")
 7         .setIcon(R.drawable.hobby)
 8         //默認選中了哪些,點擊也不會自動關閉
 9         .setMultiChoiceItems(hobby, bool, new OnMultiChoiceClickListener() {
10             @Override
11             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
12                 if(isChecked){
13                     list.add(hobby[which]);
14                 }else{
15                     list.remove(hobby[which]);
16                 }
17             }
18         })
19         .setPositiveButton("ok", new OnClickListener() {
20             @Override
21             public void onClick(DialogInterface dialog, int which) {
22                 
23                 Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
24                 
25             }
26         }).create().show();
27     }
多選對話框AlertDialog setMultiChoiceItem

 

6、適配器對話框AlertDialog setAdapter

 

 1 public void dialog6(View v){
 2         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hobby);
 3         new AlertDialog.Builder(this)
 4         .setTitle("適配器對話框")
 5         //和setItem一樣,選中之后對話框就自動消失,不需要Button
 6         .setAdapter(adapter, new OnClickListener() {
 7             @Override
 8             public void onClick(DialogInterface dialog, int which) {
 9                 Toast.makeText(MainActivity.this, hobby[which], Toast.LENGTH_SHORT).show();
10                 
11             }
12         }).create().show();
13     }
適配器對話框AlertDialog setAdapter

 

 7、自定義對話框AlertDialog setView

自定義對話框AlertDialog setView

 

8、關閉對話框AlertDialog dismisson

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5     <ImageView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:src="@drawable/ic_launcher"/>
 9     
10     <EditText
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:hint="關閉對話框請點擊關閉按鈕"/>
14     
15     <TextView
16         android:id="@+id/finish"
17         android:layout_width="wrap_content"
18            android:layout_height="wrap_content"
19            android:text="關閉"/>
20 
21 </LinearLayout>
Dialog_dismiss.xml
 1 public void dialog8(View v){
 2         View layout = getLayoutInflater().inflate(R.layout.dialog_dismiss, null);
 3         TextView finish = (TextView) layout.findViewById(R.id.finish);
 4         final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
 5         .setTitle("可關閉的對話框")
 6         .setView(layout)
 7         .create();
 8         dialog.show();
 9         finish.setOnClickListener(new View.OnClickListener() {
10             @Override
11             public void onClick(View v) {
12                 dialog.dismiss();
13             }
14         });
15         
16         
17         //有時候用戶可能點到了外部,dialog就直接關閉了,而程序不知道,這時候就需要設置
18         dialog.setCancelable(false);
19         dialog.setOnDismissListener(new OnDismissListener() {
20             @Override
21             public void onDismiss(DialogInterface dialog) {
22                 Toast.makeText(MainActivity.this, "關閉", Toast.LENGTH_SHORT).show();
23                 
24             }
25         });
26     }
關閉對話框 AlertDialog setView

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM