天天看點

安卓開發_使用AlertDialog實作對話框

示例:

一、确定對話框

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框

1 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 2             builder.setTitle("确認對話框");
 3             builder.setIcon(R.drawable.icon_72);
 4             builder.setMessage("這裡是對話框内容");
 5             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 6                 
 7                 @Override
 8                 public void onClick(DialogInterface arg0, int arg1) {
 9                     // TODO Auto-generated method stub
10                     Toast.makeText(AlertDialog_text.this, "點選了确定按鈕", 1).show();
11                 }
12             });
13             AlertDialog dialog = builder.create();
14             dialog.show(); //顯示、      

View Code

安卓開發_使用AlertDialog實作對話框

二、普通清單

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框
1 final String[] items = new String[]{"國文","數學","英語","實體","化學"};   //清單項
 2             Builder alertdialog = new AlertDialog.Builder(this); 
 3             alertdialog.setTitle("你喜歡的課程").setItems(items, new DialogInterface.OnClickListener() {
 4                 
 5                 @Override
 6                 public void onClick(DialogInterface dialog, int which) {
 7                     // TODO Auto-generated method stub
 8                     Toast.makeText(AlertDialog_lianxi.this, items[which], Toast.LENGTH_SHORT).show();
 9                     
10                 }
11             });
12             alertdialog.create().show();                  //建立顯示清單      

普通清單

安卓開發_使用AlertDialog實作對話框

三、單選清單

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框
1 final String[] items_fruit = new String[]{"蘋果","香蕉","橘子","西瓜","梨"};
 2             Builder alerdialog = new AlertDialog.Builder(this);
 3             //設定清單标題
 4             alerdialog.setTitle("你喜歡的水果");
 5             //設定單選清單
 6             alerdialog.setSingleChoiceItems(items_fruit, 0, new DialogInterface.OnClickListener() {
 7                 @Override
 8                 public void onClick(DialogInterface dialog, int which) {
 9                     // TODO Auto-generated method stub
10                     Toast.makeText(AlertDialog_lianxi.this, items_fruit[which], Toast.LENGTH_SHORT).show();
11                     
12                 }
13             });
14             //設定取消按鈕并且設定響應事件
15             alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
16                 
17                 @Override
18                 public void onClick(DialogInterface dialog, int which) {
19                     // TODO Auto-generated method stub
20                     //取消按鈕響應事件
21                 }
22             });
23             //添加确定按鈕 并且設定響應事件
24             alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {     
25                 
26                 @Override
27                 public void onClick(DialogInterface dialog, int which) {
28                     // TODO Auto-generated method stub
29                     //确定按鈕響應事件
30                 }
31             });
32             
33             alerdialog.create().show();//建立顯示清單      

單選清單

安卓開發_使用AlertDialog實作對話框

四、多選清單

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框
1 final String[] items_fruit1 = new String[]{"蘋果","香蕉","橘子","西瓜","梨"}; //設定項
 2             final boolean[] items_fruit_selected = new boolean[]{true,false,false,false,false};  
 3             Builder alerdialog1 = new AlertDialog.Builder(this);
 4             //設定清單标題
 5             alerdialog1.setTitle("你喜歡的水果");
 6             //設定多選清單
 7             alerdialog1.setMultiChoiceItems(items_fruit1, items_fruit_selected, new DialogInterface.OnMultiChoiceClickListener() {
 8                 
 9                 @Override
10                 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
11                     // TODO Auto-generated method stub
12                     items_fruit_selected[which] = isChecked;
13                 }
14             });
15             //設定取消按鈕并且設定響應事件
16             alerdialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
17                 
18                 @Override
19                 public void onClick(DialogInterface dialog, int which) {
20                     // TODO Auto-generated method stub
21                     //取消按鈕響應事件
22                 }
23             });
24             //添加确定按鈕 并且設定響應事件,将選擇的項顯示
25             alerdialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {     
26                 
27                 @Override
28                 public void onClick(DialogInterface dialog, int which) {
29                     // TODO Auto-generated method stub
30                     //确定按鈕響應事件
31                     StringBuilder stringBuilder = new StringBuilder();
32                     for(int i=0;i<items_fruit_selected.length;i++)
33                     {
34                         if(items_fruit_selected[i] == true)
35                         {
36                             stringBuilder.append(items_fruit1[i]+"、");
37                         }
38                     }
39                     Toast.makeText(AlertDialog_lianxi.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
40                 }
41             });
42             
43             alerdialog1.create().show();//建立顯示清單      

多選清單

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框

5、自定義布局對話框

安卓開發_使用AlertDialog實作對話框

對話框布局檔案

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框
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:gravity="center_horizontal"
 6     android:orientation="vertical" >
 7     
 8     <EditText 
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:editable="false"
12         android:text="自定義對話框"/>
13     
14     <TextView 
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="這裡是自定義對話框的内容"
18         android:textSize="20dp"
19         />
20     <Button 
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:text="确定"
24         />
25 </LinearLayout>      

layout_dialog

安卓開發_使用AlertDialog實作對話框
安卓開發_使用AlertDialog實作對話框
1 package com.example.demo1;
 2 
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.content.DialogInterface;
 6 import android.os.Bundle;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 
12 public class MainActivity extends Activity {
13     private Button btn_openDialog;
14     private View view;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         btn_openDialog = (Button) findViewById(R.id.id_submit);
20         btn_openDialog.setOnClickListener(new OnClickListener() {
21             
22             @Override
23             public void onClick(View v) {
24                 // TODO Auto-generated method stub
25                 view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_dialog, null);
26                 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
27                                      .setTitle("主題")
28                                      .setIcon(R.drawable.ic_launcher)
29                                      .setView(view)
30                                      .setPositiveButton("取消", new DialogInterface.OnClickListener() {
31                                         
32                                         @Override
33                                         public void onClick(DialogInterface dialog, int which) {
34                                             // TODO Auto-generated method stub
35                                             
36                                         }
37                                     })
38                                      .create();
39                 dialog.show();
40             }
41         });
42         
43         
44     }
45 
46     
47 
48 }      

MainActivity.class

繼續閱讀