对话框是人机交互过程中十分常见的组件,一般用于在特定条件下对用户显示一些信息,可以增强应用的友好性。

Dialog 类是对话框的基类。对话框虽然可以在界面上显示,但是 Dialog 不是 View 类的子类,而是直接继承自 java.lang.Object 类。

Dialog 对象也有自己的生命周期,其生命周期由创建它的 Activity 进行管理。

Activity 可以调用 showDialog(int id) 将不同 ID 的对话框显示出来,也可以调用 dismissDialog(int id)方法将 ID 标识的对话框从用户界面中关闭掉。

当 Activity 调用了 showDialog(ID)方法,对应 ID 的对话框没有被创建时,Android 系统会回调 OnCreateDialog(ID) 方法来创建具有该 ID 的对话框。

在 Activity 中创建的对话框都会被 Activity 保存,下次 showDialog(ID) 方法被调用时,若该 ID 的对话框已经被创建,则系统不会再次调用 OnCreateDialog(ID) 方法创建该对话框,而是会回调 onPrepareDialog(int id, Dialog dialog) 方法,该方法允许对话框在被显示之前做一些修改。

常用的对话框有 AlertDialog 和 ProgressDialog,下面将通过实例讲解这两种对话框的使用方法。

AlertDialog

AlertDialog 对话框是十分常用的用于显示信息的方式,最多可提供三个按钮。

AlertDialog 不能直接通过构造方法构建,而要由 AlertDialog.Builder 类来创建。

AlertDialog 对话框的标题、按钮以及按钮要响应的事件也由 AlertDialog.Builder 设置。

在使用 AlertDialog. Builder 创建对话框时常用的几个方法如下:
 

名称

作用

setTitle()

设置对话框中的标题

setIcon()

设置对话框中的图标

setMessage()

设置对话框的提示信息

setPositiveButton()

为对话框添加yes按钮

setNegativeButton()

为对话框添加no按钮

setNeutralButton()

为对话框添加第三个按钮

下面通过实例来学习创建 AlertDialog 的方法。

创建 Android 工程 DialogDemo,并在 main.xml 中添加两个按钮,分别为 AlertDialog 和 ProcessDialog。

其 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:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="Dialog演示" />
11.  
12. <Button
13. android:id="@+id/button1"
14. android:layout_width="match_parent"
15. android:layout_height="wrap_content"
16. android:text="AlertDialog" />
17.  
18. <Button
19.     android:id="@+id/button2"
20.     android:layout_width="match_parent"
21.     android:layout_height="wrap_content"
22.     android:text="ProgressDialog" />
23. </LinearLayout>

其运行效果如图 1 所示。

android Dialog是什么涉及模式 android dialog原理_android


图 1  AlertDialog的运行效果

处理 AlertDialog 按钮单击事件的代码为:


 

1. btn = (Button) findViewById(R.id.button1);
2. btn.setOnClickListener(new View.OnClickListener() {

3. @Override
4. public void onClick(View v) {

5. showDialog(Alert_DLG);
6. }
7. });

单击 AlertDialog 按钮,调用 showDialog(ALERT_DLG),系统回调 onCreateDialog(int id) 方法,创建并弹出 AlertDialog 对话框,如图 2 所示。
 

android Dialog是什么涉及模式 android dialog原理_xml_02


图 2  单击AlertDialog按钮的效果

相关代码为:


 

1. protected Dialog onCreateDialog(int id) {

2. // TODO Auto-generated method stub
3. Dialog dialog = null;
4. switch (id) {

5. case ALERT_DLG:
6. AlertDialog.Builder builder = new AlertDialog.Builder(DialogDemoActivity.this);
7. builder.setIcon(android.R.drawable.ic_dialog_info);
8. builder.setTitle("AlertDialog");
9. builder.setMessage("这是一个AlertDialog");
10. builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {

11. @Override
12. public void onClick(DialogInterface dialog, int which) {

13. // TODO Auto-generated method stub
14. Log.i("DialogDemo", "OK ! ");
15. }
16. });
17. builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {

18. @Override
19. public void onClick(DialogInterface dialog, int which) {

20. // TODO Auto-generated method stub
21. Log.i("DialogDemo", "Cancel按钮被单击! ");
22. }
23. });
24.  
25. builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {

26. @Override
27. public void onClick(DialogInterface dialog, int which) {

28. // TODO Auto-generated method stub
29. Log.i("DialogDemo", "Neutral按钮被单击!");
30. }
31. });
32. dialog = builder.create();
33. break;
34. default:
35. break;
36. }
37. return dialog;
38. }

onCreateDialog() 方法中创建了带有三个按钮的 AlertDialog,并且为每个按钮添加了事件处理方法,以便获知用户单击了哪个按钮。

ProgressDialog

ProgressDialog 是一个带有进度条的对话框,当应用程序在完成比较耗时的工作时,使用该对话框可以为用户提供一个总进度上的提示。

为 main.xml 布局中的 ProgressDialog 按钮添加事件处理代码:


 

1. Button progressbtn = (Button)findViewById(R.id.button2);
2. progressbtn.setOnClickListener(new View.OnClickListener() {

3. @Override
4. public void onClick(View view) {

5. showDialog(PROGRESS_DLG);
6. }
7. });

单击 ProgressDialog 按钮,调用 showDialog(PROGRESS_DLG) ,系统回调 onCreateDialog(int id) 方法,创建并弹出 ProgressDialog 对话框,如图 3 所示。
 

android Dialog是什么涉及模式 android dialog原理_xml_03


图 3  单击ProgressDialog按钮的效果

onCreateDialog() 方法中的相关代码如下:


1. case PROGRESS_DLG:
2. final ProgressDialog progressDialog;
3. progressDialog = new ProgressDialog(this);
4. //设置水平进度条
5. progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
6. //设置进度条最大值为100
7. progressDialog.setMax(100);
8. //设置进度条当前值为0
9. progressDialog.setProgress(0);
10. dialog = progressDialog;
11. new Thread(new Runnable() {

12. int count = 0;
13.  
14. @Override
15. public void run() {

16. // TODO Auto-generated method stub
17. while (progressDialog.getProgress() < 100) {

18. count += 3;
19. progressDialog.setProgress(count);
20. try {

21. Thread.sleep(1000);
22. } catch (InterruptedException e) {

23. // TODO Auto-generated catch block
24. e.printStackTrace();
25. }
26. }
27. }
28. }).start();
29. break;