Android中AlertDialog使用

来源:互联网 发布:欠淘宝消保 编辑:程序博客网 时间:2024/05/18 16:16

1、Dialog简介

Dialog通常是一个小窗口,出现在当前activity的前面。底层的activity失去焦点Dialog接受所有的用户交互。Dialog通常用于通知应用程序的进程应该中断用户和执行任务(如一个进度条或登录提示)

Android应用中,有多种对话框:DialogAlertDialogProgressDialog、时间、日期等对话框,他们之间的继承关系如下:

 

Dialog是一切对话框的基类,通常不应该直接实例化一个Dialog,而应该使用它的子类。Dialog虽然可以在界面上显示,但是并非继承View类。类似于Activity,Dialog也是有生命周期的,它的生命周期由Activity来维护。Activity负责生产,保存,回复它,在生命周期的每个阶段都有一些回调函数供系统方向调用。

2、AlertDialog简介

2.1、AlertDialog概述

AlertDialog标准对话框又叫警告框,是Android系统当中最常用的对话框之一。一个AlertDialog可以有零、一个、两个、三个Button、列表可选项包括复选框或单选按钮。

不能直接通过AlertDialog的构造函数来生成一个AlertDialog,一般生成AlertDialog的时候都是通过它的一个内部静态类AlertDialog.builder来构造的。

AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

2.2、AlertDialog的相关方法

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

  setTitle :为对话框设置标题
    setIcon :为对话框设置图标
    setMessage:为对话框设置内容
    setView : 给对话框设置自定义样式
    setItems :设置对话框要显示的一个list,一般用于显示几个命令时
    setMultiChoiceItems :用来设置对话框显示一系列的复选框
    setNeutralButton :普通按钮

  setPositiveButton :给对话框添加"Yes"按钮
    setNegativeButton :对话框添加"No"按钮
    create : 创建对话框

3、AlertDialog的创建

3.1、简单对话框

创建一个只有标题加内容的对话框,比如关于我们

/** * 创建一个简单对话框 */private void createDialog_1() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("this is title").setMessage("this is Content");// 获取AlertDialogAlertDialog dialog = builder.create();// 显示dialog.show();}

3.2、带按钮的AlertDialog

创建一个如下图所示的并排按钮的对话框,比如exit时的警告框

/** * 创建带按钮的对话框 */private void createDialog_2() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Are you sure to exit?").setCancelable(false)// 设置不能取消.setPositiveButton("yes", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {MainActivity.this.finish();}}).setNegativeButton("no", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}});// 获取AlertDialogAlertDialog dialog = builder.create();// 显示dialog.show();}

3.3、类似ListView的AlertDialog

使用setItems()创建普通列表对话框比如某人的一些注册信息

/** * 创建单选对话框 */private void createDialog_3_2() {final CharSequence[] items = { "Red", "Green", "Blue" };AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Pick a color");builder.setItems(items, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {mToast.setText(items[item]);mToast.show();}});AlertDialog dialog = builder.create();dialog.show();}

3.4、类似RadioButton的AlertDialog

使用setSingleChoiceItems()创建单选按钮对话框:

** * 创建单选按钮对话框 */private void createDialog_3_1() {final CharSequence[] items = { "Red", "Green", "Blue" };AlertDialog.Builder builder = new AlertDialog.Builder(this);// -1 表示默认没有选中任何项builder.setTitle("Pick a color").setSingleChoiceItems(items, -1, new OnClickListener() {public void onClick(DialogInterface dialog, int item) {index = item;}}).setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {mToast.setText(items[index]);mToast.show();}}).setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}});AlertDialog dialog = builder.create();dialog.show();}

3.5、类似CheckBox的AlertDialog

使用setMultiChoiceItems()创建单选按钮对话框:

private void createDialog_4() {final CharSequence[] items = { "Red", "Green", "Blue" };final boolean[] itemsSelected = { false, false, false };AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Pick a color")// 设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理.setMultiChoiceItems(items, itemsSelected,new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which, boolean isChecked) {// 设置选中项itemsSelected[which] = isChecked;}}).setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < itemsSelected.length; i++) {if (itemsSelected[i] == true) {stringBuilder.append(items[i] + "、");}mToast.setText("你选中了" + stringBuilder.toString());mToast.show();}}}).setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}});AlertDialog dialog = builder.create();dialog.show();}

4、自定义AlertDialog

由于系统默认对话框样式是固定、统一的,而在实际开发过程中,往往根据应用不同,可能会有不同的布局或者配色,那么就需要我们自定义对话框了。

如何自定义一个对话框呢?下面给出了三种方式。

3.1、修改系统默认的Dialog样式(风格、主题)

 

3.2、自定义Dialog布局文件

使用这种方式自定义dialog只需创建一个dialog的布局文件,然后使用buildersetView()方法设置该布局文件即可。

1、创建自定义的dialog布局文件custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/layout_dialog"    android:orientation="vertical" >    <ImageView        android:layout_width="match_parent"        android:layout_height="60dp"        android:background="#FFFFBB33"        android:contentDescription="@string/app_name"        android:scaleType="center"        android:src="@drawable/ic_launcher" />    <EditText        android:id="@+id/et_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="5dp"        android:layout_marginTop="10dp"        android:layout_marginLeft="4dp"        android:layout_marginRight="4dp"        android:hint="请输入用户名"        android:inputType="textEmailAddress" />    <EditText        android:id="@+id/et_pwd"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:layout_marginTop="5dp"        android:layout_marginLeft="4dp"        android:layout_marginRight="4dp"        android:fontFamily="sans-serif"        android:hint="请输入密码"        android:inputType="textPassword" /></LinearLayout>

2、使用BuilderLayoutInflater显示dialog

private void createDialog_5() {AlertDialog.Builder builder = new AlertDialog.Builder(this);// LayoutInflater layoutInflater = this.getLayoutInflater();LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);View view = layoutInflater.inflate(R.layout.custom_dialog, null);builder.setView(view)// Add action buttons.setPositiveButton("登录", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int id) {mToast.setText("这里写登录成功的处理");mToast.show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {mToast.setText("取消");mToast.show();}});AlertDialog dialog = builder.create();// showdialog.show();}

3.3、自定义Dialog的子类

使用该方法定义dialog需创建Dialog的子类,然后重写其构造函数,并根据实际要求添加额外的功能。

0 0
原创粉丝点击