android中常见对话框之一AlertDialog

在Android应用中,有多种对话框:Dialog、AlertDialog、ProgressDialog、时间、日期等对话框。

  (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承与习惯的View类,而是直接从java.lang.Object开始构造出来的,类似于Activity,Dialog也是有生命周期的,它的生命周期由Activity来维护。Activity负责生产,保存,回复它,在生命周期的每个阶段都有一些回调函数供系统方向调用。

  (2)AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。一个AlertDialog可以有两个Button或3个Button,可以对一个AlertDialog设置title和message.不能直接通过AlertDialog的构造函数来生成一个AlertDialog.一般生成AlertDialog的时候都是通过它的一个内部静态类AlertDialog.builder来构造的。

  (3)顾名思义,这个Dialog负责给用户显示进度的相关情况,它是AlertDialog的一个子类。

  本章我们着重讲解一下AlertDialog!

  AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。

  要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

  使用AlertDialog.Builder创建对话框需要了解以下几个方法:

    setTitle :为对话框设置标题     

      setIcon :为对话框设置图标    

   setMessage:为对话框设置内容   

   setView : 给对话框设置自定义样式  

   setItems :设置对话框要显示的一个list,一般用于显示几个命令时    

   setMultiChoiceItems :用来设置对话框显示一系列的复选框   

   setNeutralButton :普通按钮

   setPositiveButton :给对话框添加"Yes"按钮   

   setNegativeButton :对话框添加"No"按钮    

  create : 创建对话框    

  show :显示对话框

一、简单对话框

  Dialog类虽然可以在界面上显示,但是并非继承与习惯的View类,而是直接从java.lang.Object开始构造出来的。

  1、打开“src/com.genwoxue.alertdialog_a/MainActivity.java”文件。

  然后输入以下代码:

01.package com.example.alertdialog_a;  
02.  
03.import android.os.Bundle;  
04.import android.app.Activity;  
05.import android.app.AlertDialog.Builder;  
06.import android.app.AlertDialog;  
07.  
08.public class MainActivity extends Activity {  
09.  
10.    @Override  
11.    protected void onCreate(Bundle savedInstanceState) {  
12.        super.onCreate(savedInstanceState);  
13.        setContentView(R.layout.activity_main);  
14.        //AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。  
15.        //要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法  
16.        Builder adInfo=new AlertDialog.Builder(this);  
17.        adInfo.setTitle("简单对话框");                       //设置标题  
18.        adInfo.setMessage("这是一个美丽的传说,精美的石头会唱歌……");  //设置内容  
19.        adInfo.setIcon(R.drawable.ic_launcher);          //设置图标  
20.        adInfo.create();  
21.        adInfo.show();  
22.          
23.    }  
24.}  

2、运行,显示界面:

  

二、带按钮的AlertDialog

  我们在执行删除、确认等操作时,常常在对话框中单击按钮,AlertDialog可以显示3个按钮。

  1、打开“src/com.genwoxue.alertdialog_bMainActivity.java”文件。

  然后输入以下代码:

01.package com.example.alertdialog_b;  
02.  
03.import android.os.Bundle;  
04.import android.app.Activity;  
05.import android.app.AlertDialog.Builder;  
06.import android.app.AlertDialog;  
07.import android.content.DialogInterface;  
08.  
09.public class MainActivity extends Activity {  
10.  
11.    @Override  
12.    protected void onCreate(Bundle savedInstanceState) {  
13.        super.onCreate(savedInstanceState);  
14.        setContentView(R.layout.activity_main);  
15.  
16.        Builder dialog = new AlertDialog.Builder(this);  
17.        dialog.setTitle("确定删除?");  
18.        dialog.setMessage("您确定删除该条信息吗?");  
19.        dialog.setIcon(R.drawable.ic_launcher);  
20.        //为“确定”按钮注册监听事件  
21.        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
22.             @Override  
23.             public void onClick(DialogInterface dialog, int which) {  
24.                  // 根据实际情况编写相应代码。  
25.             }  
26.        });  
27.        //为“取消”按钮注册监听事件   
28.        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
29.             @Override  
30.             public void onClick(DialogInterface dialog, int which) {  
31.                  // 根据实际情况编写相应代码。  
32.             }  
33.        });  
34.        //为“查看详情”按钮注册监听事件  
35.        dialog.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {  
36.             @Override  
37.             public void onClick(DialogInterface dialog, int which) {  
38.                 // 根据实际情况编写相应代码。  
39.             }  
40.        });  
41.        dialog.create();  
42.        dialog.show();  
43.    }  
44.}  


2、运行,显示界面:

  

 

三、带有单选按钮、类似ListView的AlertDialog对话框

  setSingleChoiceItems(CharSequence[] items, int checkedItem,final OnClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第三个参数设置监听处理事件。

  1、打开“src/com.genwoxue.alertdialog_c/MainActivity.java”文件。

  然后输入以下代码:

01.package com.genwoxue.alertdialog_c;  
02.  
03.import android.app.Activity;  
04.import android.app.AlertDialog;  
05.import android.app.Dialog;  
06.import android.content.DialogInterface;  
07.import android.os.Bundle;  
08.import android.widget.Toast;  
09.  
10.public class MainActivity extends Activity {  
11.    //声明选中项变量  
12.    private int selectedCityIndex = 0;  
13.       
14.     @Override  
15.     public void onCreate(Bundle savedInstanceState) {  
16.       super.onCreate(savedInstanceState);  
17.       setContentView(R.layout.activity_main);  
18.       //定义城市数组  
19.       final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" };  
20.  
21.       //实例化AlertDialog对话框  
22.       Dialog alertDialog = new AlertDialog.Builder(this)  
23.         .setTitle("你最喜欢哪个地方?")                        //设置标题  
24.         .setIcon(R.drawable.ic_launcher)                //设置图标  
25.         //设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理  
26.         .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {  
27.       
28.           @Override  
29.           public void onClick(DialogInterface dialog, int which) {  
30.               selectedCityIndex = which;               //选中项的索引保存到选中项变量  
31.           }  
32.         })  
33.         //添加取消按钮并增加监听处理  
34.        .setNegativeButton("取消", new DialogInterface.OnClickListener() {  
35.             @Override  
36.             public void onClick(DialogInterface dialog, int which) {  
37.                // TODO Auto-generated method stub  
38.             }  
39.          })  
40.        //添加确定按钮并增加监听处理  
41.        .setPositiveButton("确认", new DialogInterface.OnClickListener() {  
42.             @Override  
43.             public void onClick(DialogInterface dialog, int which) {  
44.             Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();  
45.             }  
46.          })  
47.        .create();  
48.       alertDialog.show();  
49.     }  
50.}  


2、运行,显示界面:

  

 

四、带有复选框、类似ListView的AlertDialog对话框

  setMultiChoiceItems(CharSequence[] items, boolearn[] checkedItems,final OnMultiChoiceClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第在个参数设置监听处理事件。

  1、打开“src/com.genwoxue.alertdialog_d/MainActivity.java”文件。

  然后输入以下代码:

01.package com.genwoxue.alertdialog_d;  
02.  
03.import android.app.Activity;  
04.import android.app.AlertDialog;  
05.import android.app.Dialog;  
06.import android.content.DialogInterface;  
07.import android.os.Bundle;  
08.import android.widget.Toast;  
09.  
10.  
11.public class MainActivity extends Activity {  
12.  
13.    @Override  
14.    public void onCreate(Bundle savedInstanceState) {  
15.    super.onCreate(savedInstanceState);  
16.    setContentView(R.layout.activity_main);  
17.    //定义运动数组  
18.    final String[] arraySport = new String[] { "足球", "篮球", "网球", "乒乓球" };  
19.    final boolean[] arraySportSelected = new boolean[] {false, false, false, false};  
20.      
21.    //实例化AlertDialog对话框  
22.    Dialog alertDialog = new AlertDialog.Builder(this)  
23.       .setTitle("你喜欢哪些运动?")                        //设置标题  
24.       .setIcon(R.drawable.ic_launcher)               //设置图标  
25.       //设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理  
26.       .setMultiChoiceItems(arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener() {  
27.           
28.           @Override  
29.           public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
30.               arraySportSelected[which] = isChecked;              //选中项的布尔真假保存到选中项变量  
31.           }  
32.       })  
33.       //添加取消按钮并增加监听处理       
34.       .setPositiveButton("确认", new DialogInterface.OnClickListener() {  
35.  
36.           @Override  
37.           public void onClick(DialogInterface dialog, int which) {  
38.              StringBuilder stringBuilder = new StringBuilder();  
39.              for (int i = 0; i < arraySportSelected.length; i++) {  
40.              if (arraySportSelected[i] == true){  
41.                  stringBuilder.append(arraySport[i] + "");  
42.              }  
43.           }                
44.           Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show();  
45.         }  
46.        })  
47.         
48.        //添加确定按钮并增加监听处理       
49.       .setNegativeButton("取消", new DialogInterface.OnClickListener() {  
50.  
51.           @Override  
52.           public void onClick(DialogInterface dialog, int which) {  
53.               // TODO Auto-generated method stub  
54.           }  
55.        })        
56.       .create();  
57.      
58.     alertDialog.show();  
59.     }  
60.}  

2、运行,显示界面:

  

原文地址:https://www.cnblogs.com/jianrong-zheng/p/3248518.html