在图形界面之中,对话框也是人机交互的一种重要形式,程序可以通过对画框对用户进行一些信息的提示,而用户也可以通过对话框和程序进行一些简单的交互操作。
在Android的开发之中,所以的对话框都是从Android.app.Dialog而来的。
对话框的子类:

对话框的常用方法:

AlertDialog 和 AlertDialog.Builder
Alert 表示一个警告的含义,所以AlertDialog表示的是一个警告框的概念,主要的功能是产生一条警告信息
AlertDialog是diallog的直接子类,所以可以使用Dialog类中的各个操作方法。但是这个类的构造方法全部使用的了Protected关键字定义,所以这个关键字的定义权限特点:本类、同一包的类,不同包的子类可以访问,所以也就是意味着AlertDialog类的构造方法被隐藏了。
所以如果要想创建AlertDialog对话框,那么就必须要使用AlertDialog.Builder类完成,而通过这个类的名称就可以清楚的发现,是一个专门用于对话框的创建类。

(简单警告框)
public class MyDialogDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("对话框") // 创建标题
.setMessage("显示提示信息") // 表示对话框中的内容
.setIcon(R.drawable.pic_m) // 设置LOGO
.create(); // 创建了一个对话框
dialog.show() ; // 显示对话框
}
}
(多种操作按钮)public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setTitle("确定删除?") // 创建标题
.setMessage("您确定要删除该条信息吗?") // 表示对话框中的内容
.setIcon(R.drawable.pic_m) // 设置LOGO
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create(); // 创建了一个对话框
dialog.show() ; // 显示对话框
}
}
}
(退出按钮)public class MyDialogDemo extends Activity {
private ImageButton but = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.but = (ImageButton) super.findViewById(R.id.but) ; // 取得按钮
this.but.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
MyDialogDemo.this.exitDialog() ;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // 监听键盘
if (keyCode == KeyEvent.KEYCODE_BACK) { // 返回键
this.exitDialog() ;
}
return false ;
}
private void exitDialog(){
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setTitle("程序退出?") // 创建标题
.setMessage("您确定要退出本程序吗?") // 表示对话框中的内容
.setIcon(R.drawable.pic_m) // 设置LOGO
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyDialogDemo.this.finish() ; // 操作结束
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create(); // 创建了一个对话框
dialog.show() ; // 显示对话框
}
}
(列表对话框)public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
private TextView mych = null ; // 定义文本
private String fruitData[] = new String[] { "苹果", "西瓜", "水蜜桃" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setIcon(R.drawable.pic_m)
.setTitle("请选择你喜欢吃的水果?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setItems(MyDialogDemo.this.fruitData, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyDialogDemo.this.mych
.setText("您选择的水果是:"
+ MyDialogDemo.this.fruitData[which]);
}
}).create() ;
dialog.show() ;
}
}
}
(资源文件配置选项)以上例子:改成调用配置文件数据
Xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="fruit_labels">
<item>苹果</item>
<item>西瓜</item>
<item>水蜜桃</item>
</string-array>
</resources>
public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
private TextView mych = null ; // 定义文本
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setIcon(R.drawable.pic_m)
.setTitle("请选择你喜欢吃的水果?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setItems(R.array.fruit_labels, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyDialogDemo.this.mych
.setText("您选择的水果是:"
+ MyDialogDemo.this
.getResources()
.getStringArray(
R.array.fruit_labels)[which]);
}
}).create() ;
dialog.show() ;
}
(单选对话框)public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
private TextView mych = null ; // 定义文本
private TextView mytext = null ; // 定义文本
private String fruitData [] = new String[] { "苹果", "西瓜", "水蜜桃" };
private String fruitDesc [] = new String[] {
"苹果,植物类水果,多次花果,具有丰富的营养成分,有食疗、辅助治疗等功能。",
"西瓜(学名:Citrullus Lanatus,英文:Watermelon),属葫芦科,原产于非洲。",
"水蜜桃,在植物分类学上属于蔷薇科,梅属,桃亚属,为落叶小乔木。"} ;
private int chNum = 0 ; // 保存选项
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本
this.mytext = (TextView) super.findViewById(R.id.mytext) ; // 取得文本
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setIcon(R.drawable.pic_m)
.setTitle("请选择你喜欢吃的水果?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyDialogDemo.this.mych
.setText(MyDialogDemo.this.fruitData[MyDialogDemo.this.chNum]); // 设置选项的名称
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setSingleChoiceItems(MyDialogDemo.this.fruitData, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyDialogDemo.this.mytext
.setText(MyDialogDemo.this.fruitDesc[which]);
MyDialogDemo.this.chNum = which ; // 保存选项的索引
}
}).create() ;
dialog.show() ;
}
}
}
(复选框对话框)public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
private TextView mych = null ; // 定义文本
private String fruitData [] = new String[] { "苹果", "西瓜", "水蜜桃" };
private boolean chData[] = new boolean[] { true, true, false };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setIcon(R.drawable.pic_m)
.setTitle("请选择你喜欢吃的水果?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setMultiChoiceItems(MyDialogDemo.this.fruitData,
MyDialogDemo.this.chData,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
for (int x = 0; x < MyDialogDemo.this.fruitData.length; x++) {
if(x == which && isChecked) { // 当前选项被选中
MyDialogDemo.this.mych
.append(MyDialogDemo.this.fruitData[x]
+ "、");
}
}
}
}).create();
dialog.show() ;
}
}
}
(自定义对话框) 
public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
LayoutInflater factory = LayoutInflater.from(MyDialogDemo.this) ;
View myView = factory.inflate(R.layout.login, null) ;
Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
.setIcon(R.drawable.pic_m)
.setTitle("用户登录")
.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setView(myView).create();
dialog.show() ;
}
}
}
(日期对话框) 
public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new DatePickerDialog(MyDialogDemo.this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
TextView text = (TextView) MyDialogDemo.this.findViewById(R.id.txt) ;
text.setText("更新的日期为:" + year + "-" + monthOfYear + "-" + dayOfMonth) ;
}
},1981,8,19) ; // 默认的年、月、日
dialog.show() ; // 显示对话框
}
}
}
(时间对话框) 
public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Dialog dialog = new TimePickerDialog(MyDialogDemo.this,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView text = (TextView) MyDialogDemo.this.findViewById(R.id.txt) ;
text.setText("时间为:" + hourOfDay + ":" + minute) ;
}
},19,20,true) ;
dialog.show() ; // 显示对话框
}
}
}
(进度对话框) 

public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
final ProgressDialog proDia = ProgressDialog.show(MyDialogDemo.this,
"搜索网络", "请耐心等待...");
new Thread(){
public void run(){ // 线程的主体类
try {
Thread.sleep(3000) ; // 运行三秒
} catch (Exception e) {
} finally {
proDia.dismiss() ; // 关闭对话框
}
}
}.start() ;
proDia.show() ; // 显示对话框
}
}
}
(通过构造创建进度对话框)
public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
final ProgressDialog proDia = new ProgressDialog(MyDialogDemo.this) ;
proDia.setTitle("搜索网络") ;
proDia.setMessage("请耐心等待") ;
proDia.onStart() ; // 启动进度
new Thread(){
public void run(){ // 线程的主体类
try {
Thread.sleep(3000) ; // 运行三秒
} catch (Exception e) {
} finally {
proDia.dismiss() ; // 关闭对话框
}
}
}.start() ;
proDia.show() ; // 显示对话框
}
}
}
(水平条进度)public class MyDialogDemo extends Activity {
private Button mybut = null ; // 定义按钮
private static final int MAX_PROGRESS = 100 ; // 最大值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
final ProgressDialog proDia = new ProgressDialog(MyDialogDemo.this) ;
proDia.setTitle("搜索网络") ;
proDia.setMessage("请耐心等待") ;
proDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) ; // 水平进度条
proDia.setMax(MAX_PROGRESS) ; // 设置进度的最大值
proDia.setProgress(30) ; // 从进度30开始
proDia.setButton("后台处理", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
proDia.dismiss() ; // 关闭对话框
}
}) ;
proDia.setButton2("详细信息", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}) ;
proDia.onStart() ; // 启动进度
new Thread(){
public void run(){ // 线程的主体类
for (int x = 0; x < MAX_PROGRESS; x++) {
try {
Thread.sleep(500); // 运行三秒
} catch (Exception e) {
}
proDia.incrementProgressBy(1) ;
}
proDia.dismiss() ;
}
}.start() ;
proDia.show() ; // 显示对话框
}
}
课后问题对话框创建有哪几个步骤?
1创建builder
2设置属性
2.1 头部:标题,图标
2.2 身体:显示内容
2.3 尾部:按钮
3创建dialog
4显示dialog
如何创建自定义对话框?
创建布局文件 xml
得到布局加载器
用布局加载器加载布局
setView()
创建,显示即可。
225

被折叠的 条评论
为什么被折叠?



