Android——AlertDialog及其子类

Android提供了功能丰富,应用广泛的对话框:AlertDialog。
类图如下:

AlertDialog

AlertDialog对话框分为三个区域:

1.标题区。包括图标和文本,setIcon()设置图标,setTitle()设置文本。

2.内容区。可以是简单的文本,也可以是复杂的View。AlertDialog提供了6个方法来设置不同的内容。

        1)setMessage(),简单的文本。

        2)setItems(),简单列表项。

        3)setSingleChoiceItems(),单选列表项。

        4)setMultiChoiceItems(),多选列表项。

        5)setAdapter(),自定义列表项。

        6)setView(),自定义View。

3.按钮区。可通过三个方法分别设置三个按钮,setPositiveButton()、setNegativeButton()或setNeutralButton()。

下面通过一个例子展示6种不同内容的AlertDialog。

Activity类:

[java]  view plain  copy
  1. package com.zzj.ui.dialogdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.DialogInterface;  
  9. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  10. import android.os.Bundle;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.EditText;  
  15. import android.widget.Toast;  
  16.   
  17. import com.zzj.ui.R;  
  18.   
  19. public class DialogDemoActivity extends Activity {  
  20.     private String singleChoice;  
  21.     private List<String> multiChoices = new ArrayList<String>();  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.   
  27.         setContentView(R.layout.dialogdemo_activity);  
  28.     }  
  29.   
  30.     public void click(View v) {  
  31.         AlertDialog.Builder builder = new AlertDialog.Builder(  
  32.                 DialogDemoActivity.this);  
  33.         builder.setIcon(R.drawable.ic_launcher);  
  34.   
  35.         switch (v.getId()) {  
  36.         case R.id.text_dialog:// 简单对话框  
  37.             builder.setTitle("提示");  
  38.             builder.setMessage("你想干什么?");  
  39.             builder.setPositiveButton("保存",  
  40.                     new DialogInterface.OnClickListener() {  
  41.   
  42.                         @Override  
  43.                         public void onClick(DialogInterface dialog, int which) {  
  44.                             Toast.makeText(DialogDemoActivity.this,  
  45.                                     "你点击了 -- > " + which, Toast.LENGTH_LONG)  
  46.                                     .show();  
  47.                             return;  
  48.                         }  
  49.                     });  
  50.             builder.setNegativeButton("删除",  
  51.                     new DialogInterface.OnClickListener() {  
  52.   
  53.                         @Override  
  54.                         public void onClick(DialogInterface dialog, int which) {  
  55.                             Toast.makeText(DialogDemoActivity.this,  
  56.                                     "你点击了 -- > " + which, Toast.LENGTH_LONG)  
  57.                                     .show();  
  58.                             return;  
  59.                         }  
  60.                     });  
  61.             builder.setNeutralButton("取消",  
  62.                     new DialogInterface.OnClickListener() {  
  63.   
  64.                         @Override  
  65.                         public void onClick(DialogInterface dialog, int which) {  
  66.                             Toast.makeText(DialogDemoActivity.this,  
  67.                                     "你点击了 -- > " + which, Toast.LENGTH_LONG)  
  68.                                     .show();  
  69.                             return;  
  70.                         }  
  71.                     });  
  72.             break;  
  73.         case R.id.list_dialog:// 列表对话框  
  74.             builder.setTitle("请选择省份");  
  75.             final String[] items = new String[] { "湖南""广东""北京""上海""辽宁" };  
  76.             builder.setItems(items, new DialogInterface.OnClickListener() {  
  77.   
  78.                 @Override  
  79.                 public void onClick(DialogInterface dialog, int which) {  
  80.                     Toast.makeText(DialogDemoActivity.this,  
  81.                             "你选择了 -- > " + items[which], Toast.LENGTH_LONG)  
  82.                             .show();  
  83.                     return;  
  84.                 }  
  85.             });  
  86.             builder.setNegativeButton("取消",  
  87.                     new DialogInterface.OnClickListener() {  
  88.   
  89.                         @Override  
  90.                         public void onClick(DialogInterface dialog, int which) {  
  91.                             return;  
  92.                         }  
  93.                     });  
  94.             break;  
  95.         case R.id.single_choice:// 单选对话框  
  96.             singleChoice = null;  
  97.             builder.setTitle("请选择省份");  
  98.             final String[] singleItems = new String[] { "湖南""广东""北京""上海",  
  99.                     "辽宁" };  
  100.             builder.setSingleChoiceItems(singleItems, 0,  
  101.                     new DialogInterface.OnClickListener() {  
  102.   
  103.                         @Override  
  104.                         public void onClick(DialogInterface dialog, int which) {  
  105.                             singleChoice = singleItems[which];  
  106.                             return;  
  107.                         }  
  108.                     });  
  109.             singleChoice = singleItems[0];  
  110.             builder.setPositiveButton("确定",  
  111.                     new DialogInterface.OnClickListener() {  
  112.   
  113.                         @Override  
  114.                         public void onClick(DialogInterface dialog, int which) {  
  115.                             if (singleChoice != null) {  
  116.                                 Toast.makeText(DialogDemoActivity.this,  
  117.                                         "你选择了 -- > " + singleChoice,  
  118.                                         Toast.LENGTH_LONG).show();  
  119.                             }  
  120.                             return;  
  121.                         }  
  122.                     });  
  123.             builder.setNegativeButton("取消",  
  124.                     new DialogInterface.OnClickListener() {  
  125.   
  126.                         @Override  
  127.                         public void onClick(DialogInterface dialog, int which) {  
  128.                             singleChoice = null;  
  129.                             return;  
  130.                         }  
  131.                     });  
  132.             break;  
  133.         case R.id.muti_choice:// 多选对话框  
  134.             multiChoices.clear();  
  135.             builder.setTitle("请选择省份");  
  136.             final String[] multiItems = new String[] { "湖南""广东""北京""上海",  
  137.                     "辽宁" };  
  138.             builder.setMultiChoiceItems(multiItems, new boolean[] { true,  
  139.                     falsefalsefalsefalse },  
  140.                     new OnMultiChoiceClickListener() {  
  141.   
  142.                         @Override  
  143.                         public void onClick(DialogInterface dialog, int which,  
  144.                                 boolean isChecked) {  
  145.                             if (isChecked) {  
  146.                                 multiChoices.add(multiItems[which]);  
  147.                             } else {  
  148.                                 multiChoices.remove(multiItems[which]);  
  149.                             }  
  150.                             return;  
  151.                         }  
  152.                     });  
  153.             multiChoices.add(multiItems[0]);  
  154.             builder.setPositiveButton("确定",  
  155.                     new DialogInterface.OnClickListener() {  
  156.   
  157.                         @Override  
  158.                         public void onClick(DialogInterface dialog, int which) {  
  159.                             if (!multiChoices.isEmpty()) {  
  160.                                 Toast.makeText(DialogDemoActivity.this,  
  161.                                         "你选择了 -- > " + multiChoices,  
  162.                                         Toast.LENGTH_LONG).show();  
  163.                             }  
  164.                             return;  
  165.                         }  
  166.                     });  
  167.             builder.setNegativeButton("取消",  
  168.                     new DialogInterface.OnClickListener() {  
  169.   
  170.                         @Override  
  171.                         public void onClick(DialogInterface dialog, int which) {  
  172.                             multiChoices.clear();  
  173.                             return;  
  174.                         }  
  175.                     });  
  176.             break;  
  177.         case R.id.custom_item:// 自定义列表项对话框  
  178.             builder.setTitle("请选择省份");  
  179.             final String[] customItems = new String[] { "湖南""广东""北京""上海",  
  180.                     "辽宁" };  
  181.             builder.setAdapter(new ArrayAdapter<String>(  
  182.                     DialogDemoActivity.this,  
  183.                     android.R.layout.simple_list_item_1, customItems),  
  184.                     new DialogInterface.OnClickListener() {  
  185.   
  186.                         @Override  
  187.                         public void onClick(DialogInterface dialog, int which) {  
  188.                             Toast.makeText(DialogDemoActivity.this,  
  189.                                     "你选择了 --> " + customItems[which],  
  190.                                     Toast.LENGTH_LONG).show();  
  191.                             return;  
  192.                         }  
  193.                     });  
  194.             builder.setPositiveButton("取消",  
  195.                     new DialogInterface.OnClickListener() {  
  196.   
  197.                         @Override  
  198.                         public void onClick(DialogInterface dialog, int which) {  
  199.                             return;  
  200.                         }  
  201.                     });  
  202.             break;  
  203.         case R.id.custom_view: // 自定义View对话框  
  204.             builder.setTitle("登录");  
  205.             LayoutInflater inflater = LayoutInflater  
  206.                     .from(DialogDemoActivity.this);  
  207.             final View view = inflater.inflate(R.layout.dialog_custom_view,  
  208.                     null);  
  209.             builder.setView(view);  
  210.             builder.setPositiveButton("登录",  
  211.                     new DialogInterface.OnClickListener() {  
  212.   
  213.                         @Override  
  214.                         public void onClick(DialogInterface dialog, int which) {  
  215.                             String username = ((EditText) view  
  216.                                     .findViewById(R.id.edit_username))  
  217.                                     .getText().toString();  
  218.                             String password = ((EditText) view  
  219.                                     .findViewById(R.id.edit_password))  
  220.                                     .getText().toString();  
  221.                             if ("001".equals(username)  
  222.                                     && "123".equals(password)) {  
  223.                                 Toast.makeText(DialogDemoActivity.this,  
  224.                                         "登录成功!", Toast.LENGTH_LONG).show();  
  225.                             } else {  
  226.                                 Toast.makeText(DialogDemoActivity.this,  
  227.                                         "用户名或密码错误!", Toast.LENGTH_LONG).show();  
  228.                             }  
  229.                             return;  
  230.                         }  
  231.                     });  
  232.             builder.setNegativeButton("取消",  
  233.                     new DialogInterface.OnClickListener() {  
  234.   
  235.                         @Override  
  236.                         public void onClick(DialogInterface dialog, int which) {  
  237.                             return;  
  238.                         }  
  239.                     });  
  240.             break;  
  241.         default:  
  242.             break;  
  243.         }  
  244.   
  245.         // builder.show();  
  246.         builder.create().show();  
  247.     }  
  248. }  
布局文件:
[html]  view plain  copy
  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.     <Button  
  8.         android:id="@+id/text_dialog"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:onClick="click"  
  12.         android:text="简单文本对话框" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/list_dialog"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:onClick="click"  
  19.         android:text="列表对话框" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/single_choice"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:onClick="click"  
  26.         android:text="单选列表框" />  
  27.   
  28.     <Button  
  29.         android:id="@+id/muti_choice"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:onClick="click"  
  33.         android:text="多选列表框" />  
  34.   
  35.     <Button  
  36.         android:id="@+id/custom_item"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:onClick="click"  
  40.         android:text="自定义列表项对话框" />  
  41.   
  42.     <Button  
  43.         android:id="@+id/custom_view"  
  44.         android:layout_width="wrap_content"  
  45.         android:layout_height="wrap_content"  
  46.         android:onClick="click"  
  47.         android:text="自定义View对话框" />  
  48.   
  49. </LinearLayout>  

自定义View布局文件dialog_custom_view.xml:

[html]  view plain  copy
  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.     <EditText  
  8.         android:id="@+id/edit_username"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:inputType="text"  
  12.         android:hint="请输入用户名"  
  13.         android:ems="10" >  
  14.     </EditText>  
  15.   
  16.     <EditText  
  17.         android:id="@+id/edit_password"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:inputType="textPassword"  
  21.         android:hint="请输入密码"  
  22.         android:ems="10" />  
  23.   
  24. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值