源码教程 2025年06月7日
0 收藏 0 点赞 836 浏览 3998 个字
摘要 :

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

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

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

  7. android:layout_width="fill_parent"

  8. android:layout_height="wrap_content"

  9. android:text="Dialog演示" />

  10. <Button

  11. android:id="@+id/button1"

  12. android:layout_width="match_parent"

  13. android:layout_height="wrap_content"

  14. android:text="AlertDialog" />

  15. <Button

  16.     android:id="@+id/button2"

  17.     android:layout_width="match_parent"

  18.     android:layout_height="wrap_content"

  19.     android:text="ProgressDialog" />

  20. </LinearLayout>

其运行效果如图 1 所示。

Android对话框控件AlertDialog和ProgressDialog使用

图 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对话框控件AlertDialog和ProgressDialog使用

图 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. builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {

  25. @Override

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

  27. // TODO Auto-generated method stub

  28. Log.i("DialogDemo", "Neutral按钮被单击!");

  29. }

  30. });

  31. dialog = builder.create();

  32. break;

  33. default:

  34. break;

  35. }

  36. return dialog;

  37. }

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对话框控件AlertDialog和ProgressDialog使用

图 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. @Override

  14. public void run() {

  15. // TODO Auto-generated method stub

  16. while (progressDialog.getProgress() < 100) {

  17. count += 3;

  18. progressDialog.setProgress(count);

  19. try {

  20. Thread.sleep(1000);

  21. } catch (InterruptedException e) {

  22. // TODO Auto-generated catch block

  23. e.printStackTrace();

  24. }

  25. }

  26. }

  27. }).start();

  28. break;

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/994.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

309
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,040
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

836
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号