在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”文件。
然后输入以下代码:
package com.example.alertdialog_a;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.AlertDialog;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
//要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法
Builder adInfo=new AlertDialog.Builder(this);
adInfo.setTitle("简单对话框"); //设置标题
adInfo.setMessage("这是一个美丽的传说,精美的石头会唱歌……"); //设置内容
adInfo.setIcon(R.drawable.ic_launcher); //设置图标
adInfo.create();
adInfo.show();
}
}
2、运行,显示界面:
二、带按钮的AlertDialog
我们在执行删除、确认等操作时,常常在对话框中单击按钮,AlertDialog可以显示3个按钮。
1、打开“src/com.genwoxue.alertdialog_bMainActivity.java”文件。
然后输入以下代码:
package com.example.alertdialog_b;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("确定删除?");
dialog.setMessage("您确定删除该条信息吗?");
dialog.setIcon(R.drawable.ic_launcher);
//为“确定”按钮注册监听事件
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 根据实际情况编写相应代码。
}
});
//为“取消”按钮注册监听事件
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 根据实际情况编写相应代码。
}
});
//为“查看详情”按钮注册监听事件
dialog.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 根据实际情况编写相应代码。
}
});
dialog.create();
dialog.show();
}
}
2、运行,显示界面:
三、带有单选按钮、类似ListView的AlertDialog对话框
setSingleChoiceItems(CharSequence[] items, int checkedItem,final OnClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第三个参数设置监听处理事件。
1、打开“src/com.genwoxue.alertdialog_c/MainActivity.java”文件。
然后输入以下代码:
package com.genwoxue.alertdialog_c;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
//声明选中项变量
private int selectedCityIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义城市数组
final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" };
//实例化AlertDialog对话框
Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你最喜欢哪个地方?") //设置标题
.setIcon(R.drawable.ic_launcher) //设置图标
//设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理
.setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedCityIndex = which; //选中项的索引保存到选中项变量
}
})
//添加取消按钮并增加监听处理
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
//添加确定按钮并增加监听处理
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog.show();
}
}
2、运行,显示界面:
四、带有复选框、类似ListView的AlertDialog对话框
setMultiChoiceItems(CharSequence[] items, boolearn[] checkedItems,final OnMultiChoiceClickListener listener)方法来实现类似ListView的AlertDialog,第一个参数是要显示的数据的数组,第二个参数指定默认选中项,第在个参数设置监听处理事件。
1、打开“src/com.genwoxue.alertdialog_d/MainActivity.java”文件。
然后输入以下代码:
package com.genwoxue.alertdialog_d;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义运动数组
final String[] arraySport = new String[] { "足球", "篮球", "网球", "乒乓球" };
final boolean[] arraySportSelected = new boolean[] {false, false, false, false};
//实例化AlertDialog对话框
Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你喜欢哪些运动?") //设置标题
.setIcon(R.drawable.ic_launcher) //设置图标
//设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理
.setMultiChoiceItems(arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arraySportSelected[which] = isChecked; //选中项的布尔真假保存到选中项变量
}
})
//添加取消按钮并增加监听处理
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arraySportSelected.length; i++) {
if (arraySportSelected[i] == true){
stringBuilder.append(arraySport[i] + "、");
}
}
Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
})
//添加确定按钮并增加监听处理
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
alertDialog.show();
}
}
2、运行,显示界面: