Android 自学之对话框

Android为我们提供了丰富的对话框支持,提供了四种常用的对话框:

  1. AlertDialog:功能丰富、实际应用最广泛的对话框。

  2. ProgressDialog:进度对话框,该对话框只用于简单的进度条封装。

  3. DatePickerDialog:日期选择对话框,该对话框只对DatePicker包装。

  4. TimePickerDialog:时间选择对话框,该对话框只对TimePicker包装。

上面四种对话框中功能最强用法最灵活的就是AlertDialog,这里详细的介绍下AlertDialog。

一、使用AlertDialog创建简单的对话框

AlertDialog的功能强大,它提供了一些方法来生成四种预定义对话框,这四种对话框分别是:

  1. 带消息、带N个按钮的提示对话框
  2. 带列表、带N个按钮的列表对话框
  3. 带多个单选列表项,带N个按钮的对话框
  4. 带多个多选列表项,带N个按钮的对话框

除此以外,AlertDialog也可以创建界面自定义的对话框。

 

使用AlertDialog创建对话框的步骤大致如下:

  1. 创建AlertDialog.Builder对象,该对象是AlertDialog的创建器
  2. 调用AlertDialog.Builder的方法为对话框设置图标、标题、内容
  3. 调用AlertDialog.Builder的create()方法创建AlertDialog对话框
  4. 调用AlertDialog的show()方法显示对话框

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

setTitle():为对话框设置标题

setIcon():为对话框设置图标

setMessage():为对话框设置内容

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

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

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

setNeutralButton()    :普通按钮

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

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

create() : 创建对话框

show() :显示对话框

 

 

 

 

 

 

 

 

 

 

下面通过一个显示提示消息对话框的案例,来看看AlertDialog的用法:程序的界面上有个一个文本框和一个按钮,当用户点击按钮的时候将会显示普通对话框。

先看看布局文件:Layout/main.xml

 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/show"

 9         android:layout_width="fill_parent"

10         android:layout_height="wrap_content"

11         android:editable="false"/>

12 <!-- android:editable设置是否可编辑 -->

13 

14     <Button 

15         android:id="@+id/bn01"

16         android:layout_width="wrap_content"

17         android:layout_height="wrap_content"

18         android:text="显示对话框"/>

19 </LinearLayout>

主程序文件:DialogTest.java

 1 package com.yangjing.dialogtest;

 2 

 3 import android.app.Activity;

 4 import android.app.AlertDialog;

 5 import android.app.AlertDialog.Builder;

 6 import android.content.DialogInterface;

 7 import android.content.DialogInterface.OnClickListener;

 8 import android.os.Bundle;

 9 import android.view.View;

10 import android.widget.Button;

11 import android.widget.EditText;

12 

13 

14 

15 public class DialogTest extends Activity{

16     

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.main);

21         Button bn = (Button) findViewById(R.id.bn01);

22         //定义一个AlertDialog.Builder对象

23         final Builder builder = new AlertDialog.Builder(this);

24         //为按钮绑定事件监听器

25         bn.setOnClickListener(new View.OnClickListener() {

26             

27                 @Override

28                 public void onClick(View source) {

29                     // 设置对话框的图标

30                     builder.setIcon(R.drawable.ic_launcher);

31                     // 设置对话框的标题

32                     builder.setTitle("自定义普通的消息提示对话框");

33                     // 设置对话框显示的内容

34                     builder.setMessage("这是一个由AlertDialog定义出来的普通对话框");

35                     // 为对话框设置一个“确定”按钮

36                     builder.setPositiveButton(

37                             "确定",

38                             //为列表项的单击事件设置监听器

39                             new OnClickListener() {

40                             @Override

41                             public void onClick(DialogInterface arg0, int arg1) {

42                                 EditText show = (EditText) findViewById(R.id.show);

43                                 show.setText("您刚刚点击了确定按钮!");

44                             }

45 

46                     });

47                     // 为对话框设置一个“取消”按钮

48                     builder.setNegativeButton(

49                             "取消",new OnClickListener() {

50                                 

51                         @Override

52                         public void onClick(DialogInterface arg0, int arg1) {

53                             EditText show = (EditText) findViewById(R.id.show);

54                             show.setText("您刚刚点击了取消按钮!");

55                         }

56                     });

57                     

58                     builder.create().show();

59                 }

60             

61         });

62     }

63 }

运行的效果:                              

当用户点击了确定后,界面上EditText上会显示:您刚刚点击了确定按钮!,若是点了取消按钮的话,则会显示:您刚刚点击了取消按钮!

二、使用AlertDialog创建列表的对话框

AlertDialog.Builder除了提供setMessage方法来设置对话框所显示的消息以外,还提供了如下方法来设置对话框显示列表内容:

  • setItems(int itemsId,DialogInterface.OnClickListener listener): 创建普通列表对话框
  • setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceListener listener): 创建多选列表对话框
  • setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener): 创建单选列表对话框
  • setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener): 创建根据ListAdapter提供的列表项的列表对话框

1、下面通过一个普通的列表对话框的案例,来看看setItems(int itemsId,DialogInterface.OnClickListener listener)方法的用法:

程序的界面上有个一个文本框和一个按钮,当用户点击按钮的时候将会改变文本框的背景颜色

先看看我们简单的布局文件:Layout/main.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="fill_parent"

 4     android:layout_height="fill_parent"

 5     android:orientation="vertical" >

 6     

 7 <TextView

 8     android:id="@+id/show" 

 9     android:layout_width="fill_parent" 

10     android:layout_height="wrap_content" 

11     android:text="根据你选择的颜色而发生改变"

12     android:textSize="11pt"

13     />

14 <Button 

15     android:id="@+id/bn" 

16     android:layout_width="wrap_content" 

17     android:layout_height="wrap_content" 

18     android:text="选择颜色"

19     />    

20 </LinearLayout>

主程序:ListDialogTest.java

package com.yangjing.listdialog;



import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;



public class ListDialogTest extends Activity{

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button bn = (Button) findViewById(R.id.bn);

        final Builder builder = new AlertDialog.Builder(this);

        bn.setOnClickListener(new View.OnClickListener() {

            

            @Override

            public void onClick(View arg0) {

                //设置对话框的图标

                builder.setIcon(R.drawable.ic_launcher);

                //设置对话框的标题

                builder.setTitle("简单列表对话框");

                //为列表框设置多个列表

                //setItems(int itemsId,DialogInterface.OnClickListener listener): 创建普通列表对话框

                builder.setItems(

                    new String[]{"红色","绿色","蓝色"},

                    new OnClickListener() {

                        //该方法的which参数代表用户单击了那个列表项

                        @Override

                        public void onClick(DialogInterface dialog, int which) {

                                TextView show = (TextView) findViewById(R.id.show);

                                switch (which) {

                                case 0:

                                    show.setBackgroundColor(Color.RED);

                                    break;

 

                                case 1:

                                    show.setBackgroundColor(Color.GREEN);

                                    break;

                                    

                                case 2:

                                    show.setBackgroundColor(Color.BLUE);

                                    break;

                                }

                        }

                    }

                );

                builder.create().show();

            }

        });

    }

}

效果展示:

 

 2、下面通过一个单选列表对话框的案例,来看看setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)方法的用法:

通过上面的例子,只要调用了AlertDialog.Builder的setSingleChoiceItems方法既可创建一个单选列表对话框出来。

下面的案例将采用另一种方式来创建对话框,采用基于Activity回调的方式来开发对话框;其步骤如下:

  1. 重写Activity的onCreateDialog()方法,该方法会返回一个对话框,该方法的内部一样通过AlertDialog.Builder或DatePickerDialog等创建对话框并返回。
  2. 程序需要显示对话框时调用Activity的showDialog()方法。

下面先看看简单的布局文件:Layout/main.xml

 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 <TextView

 7     android:id="@+id/show" 

 8     android:layout_width="fill_parent" 

 9     android:layout_height="wrap_content" 

10     android:text="根据选中的颜色而改变,默认是绿色!"

11     android:textSize="11pt"

12     android:background="#ff00ff00"

13     />

14 <Button 

15     android:id="@+id/bn" 

16     android:layout_width="wrap_content" 

17     android:layout_height="wrap_content" 

18     android:text="选择颜色"

19     />    

20 

21 </LinearLayout>

主程序:SingleTest.java

package com.yangjing.singlechoicedialog;



import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;



public class SingleTest extends Activity {

    

    final int SING_DIALOG = 0X113;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Builder builder = new AlertDialog.Builder(this);

        Button bn = (Button) findViewById(R.id.bn);

        bn.setOnClickListener(new View.OnClickListener() {

            

            @Override

            public void onClick(View arg0) {

                showDialog(SING_DIALOG);

            }

        });

        

    }

    

    @Override

    protected Dialog onCreateDialog(int id, Bundle args) {

        //判断生成哪种类型的对话框

        switch (id) {

        case SING_DIALOG:

            Builder b = new AlertDialog.Builder(this);

            //设置对话框的图标

            b.setIcon(R.drawable.ic_launcher);

            //设置对话框的标题

            b.setTitle("单选列表对话框");

            //为对话框设置多个列表

            b.setSingleChoiceItems(

                    //列表数组

                    new String[]{"红色","绿色","蓝色"},

                    //默认的选中第几项

                    1,

                    new OnClickListener() {

                        

                        @Override

                        public void onClick(DialogInterface arg0, int arg1) {

                            TextView show = (TextView) findViewById(R.id.show);

                            switch (arg1) {

                            case 0:

                                show.setBackgroundColor(Color.RED);

                                break;



                            case 1:

                                show.setBackgroundColor(Color.GREEN);

                                break;

                                

                            case 2:

                                show.setBackgroundColor(Color.BLUE);

                                break;

                            }

                        }

                    });

            //添加一个按钮,用于关闭对话框

            b.setPositiveButton("submit", null);

            return b.create();

        }

        return null;

    }

}

运行的效果:列表中的值只要选中文本框就会发生改变,那个按钮只是用于关闭列表提示框

 

3、下面通过一个单选列表对话框的案例,来看看setMultiChoiceItems(CharSequence[] items,boolean[] checkedItems, DialogInterface.OnMultiChoiceListener listener)方法的用法:

只要调用AlertDialog.Builder的setMultiChoiceItems方法即可创建一个多选列表的对话框。

setMultiChoiceItems方法参数说明:第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个item的触发事件

由于创建多选列表框和单选列表框很相似,所以这儿就不做过多的介绍我们直接看代码。

先看看我们的界面布局文件:Layout/main.xml

 

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:orientation="vertical"

 4     android:layout_width="fill_parent"

 5     android:layout_height="fill_parent"

 6     android:gravity="center_horizontal"

 7     >

 8 <EditText

 9     android:id="@+id/show" 

10     android:layout_width="fill_parent" 

11     android:layout_height="wrap_content" 

12     android:editable="false"

13     android:textSize="11pt"

14     />

15 <Button 

16     android:id="@+id/bn" 

17     android:layout_width="wrap_content" 

18     android:layout_height="wrap_content" 

19     android:text="选择您喜欢的颜色"

20     />    

21 </LinearLayout>

 

主程序:MultiChoiceDialog.java

 1 package com.yangjing.multichoicedialog;

 2 

 3 import android.app.Activity;

 4 import android.app.AlertDialog;

 5 import android.app.Dialog;

 6 import android.app.AlertDialog.Builder;

 7 import android.content.DialogInterface;

 8 import android.content.DialogInterface.OnMultiChoiceClickListener;

 9 import android.os.Bundle;

10 import android.view.View;

11 import android.widget.Button;

12 import android.widget.EditText;

13 

14 public class MultiChoiceDialog extends Activity{

15     final int SINGLE_DIALOG = 0x113;

16     String[] colorNames = new String[]{"红色","绿色","蓝色"};

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.main);

21         Button bn = (Button) findViewById(R.id.bn);

22         bn.setOnClickListener(new View.OnClickListener() {

23             

24             @Override

25             public void onClick(View arg0) {

26                 showDialog(SINGLE_DIALOG);

27             }

28         });

29     }

30     

31     @Override

32     protected Dialog onCreateDialog(int id, Bundle args) {

33         switch (id) {

34         case SINGLE_DIALOG:

35             Builder b = new AlertDialog.Builder(this);

36             //设置对话框的图标

37             b.setIcon(R.drawable.ic_launcher);

38             //设置对话框的标题

39             b.setTitle("多选列表对话框");

40             final boolean[] checkStatus = new boolean[] { false, false, false };

41             // 为对话框设置多个列表

42             b.setMultiChoiceItems(new String[]{"红色","绿色","蓝色"} , checkStatus, new OnMultiChoiceClickListener() {

43                 

44                 @Override

45                 public void onClick(DialogInterface arg0, int arg1, boolean arg2) {

46                     EditText show = (EditText) findViewById(R.id.show);

47                     String result = "您喜欢的颜色为:";

48                     for (int i = 0; i < checkStatus.length; i++) {

49                         if (checkStatus[i]) {

50                             result += colorNames[i] + "、";

51                         }

52                     }

53                     

54                     show.setText(result);

55                 }

56             });

57             // 添加一个“确定”按钮,用于关闭该对话框

58             b.setPositiveButton("确定", null);

59             // 创建对话框

60             return b.create();

61         }

62         

63         return null;

64     }

65 }

运行效果:

 

你可能感兴趣的:(android)