android之alertdialog

本文介绍了Android中Dialog的基类及其生命周期,重点关注了AlertDialog和ProgressDialog的使用。通过示例展示了如何创建AlertDialog,包括设置按钮、标题和消息。还演示了如何创建ProgressDialog用于显示下载进度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在开始此次讲解之前,先弄清几个概念:

1:Dialog

     Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承与习惯的View类,而是直接从java.lang.Object开始构造出来的,类似于Activity,Dialog也是有生命周期的,它的生命周期由Activity来维护。Activity负责生产,保存,回复它,在生命周期的每个阶段都有一些回调函数供系统方向调用。

    在Activity当中用户可以主动调用的函数为:

  •  showDialog(int id),负责显示标示为id的Dialog。这个函数如果调用后,系统将反向调用Dialog的回调函数onCreateDialog(int id).
  • dismissDialog(int id),使标识为id的Dialog在界面当中消失。

    Dialog有两个比较常见的回调函数,onCreateDialog(int id)和onPrepareDialog(int id,Dialog dialog)函数。当在Activity当中调用onCreateDialog(int id)后,如果这个Dialog是第一次生成,系统将反向调用Dialog的回调函数onCreateDialog(int id),然后再调用onPrepareDialog(int id, Dialog dialog);如果这个Dialog已经生成,只不过没有显示出啦u,那么将不会回调onCreateDialog(int id),而是直接回调onPrepareDialog(int id,Dialog dialog)方法。

    onPrepareDialog(int id,Dialog dialog)方法提供了这样一套机制,即当Dialog生成但是没有显示出来的时候,使得有机会在显示前对Dialog做一些修改,如果Dialog标题进行修改等。

2:AlertDialog

     AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。一个AlertDialog可以有两个Button或3个Button,可以对一个AlertDialog设置title和message.不能直接通过AlertDialog的构造函数来生成一个AlertDialog.一般生成AlertDialog的时候都是通过它的一个内部静态类AlertDialog.builder来构造的。

3:ProgressDialog

     顾名思义,这个Dialog负责给用户显示进度的相关情况,它是AlertDialog的一个子类。在这个例子当中。我们实现默认的进度显示,当然可以配置自己的进度条。看一下ActivityMain当中的buildDialog4()函数,具体实现代码如下所示:

Java代码 复制代码  收藏代码
  1. private Dialog buildDialog4(Context context){   
  2.         ProgressDialog dialog= new ProgressDialog(context);   
  3.         dialog.setTitle("正在下载歌曲");   
  4.      dialog.setMessage("请稍后......");   
  5.         return dialog   
  6. }  
private Dialog buildDialog4(Context context){
        ProgressDialog dialog= new ProgressDialog(context);
        dialog.setTitle("正在下载歌曲");
     dialog.setMessage("请稍后......");
        return dialog
}

 

下面是具体的详细代码:

Java代码 复制代码  收藏代码
  1. package com.eoeAndroid.dialog;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.AlertDialog;   
  5. import android.app.Dialog;   
  6. import android.app.ProgressDialog;   
  7. import android.content.Context;   
  8. import android.content.DialogInterface;   
  9. import android.os.Bundle;   
  10. import android.view.LayoutInflater;   
  11. import android.view.View;   
  12. import android.view.View.OnClickListener;   
  13. import android.widget.Button;   
  14.   
  15. public class ActivityMain extends Activity {   
  16.     private static final int DIALOG1 = 1;   
  17.     private static final int DIALOG2 = 2;   
  18.     private static final int DIALOG4 = 4;   
  19.     private static final int DIALOG3 = 3;   
  20.   
  21.   
  22.     @Override  
  23.     protected Dialog onCreateDialog(int id) {   
  24.         switch (id) {   
  25.         case DIALOG1:   
  26.             return buildDialog1(ActivityMain.this);   
  27.   
  28.         case DIALOG2:   
  29.             return buildDialog2(ActivityMain.this);   
  30.   
  31.         case DIALOG3:   
  32.             return buildDialog3(ActivityMain.this);   
  33.         case DIALOG4:   
  34.             return buildDialog4(ActivityMain.this);   
  35.   
  36.         }   
  37.         return null;   
  38.     }   
  39.        
  40.     protected void onPrepareDialog(int id, Dialog dialog){   
  41.         if(id==DIALOG1){   
  42.             setTitle("测试");   
  43.         }   
  44.     }   
  45.   
  46.     @Override  
  47.     protected void onCreate(Bundle savedInstanceState) {   
  48.         super.onCreate(savedInstanceState);   
  49.         setContentView(R.layout.alert_dialog);   
  50.   
  51.         Button button1 = (Button) findViewById(R.id.button1);   
  52.         button1.setOnClickListener(new OnClickListener() {   
  53.             public void onClick(View v) {   
  54.                 showDialog(DIALOG1);   
  55.             }   
  56.         });   
  57.   
  58.         Button buttons2 = (Button) findViewById(R.id.buttons2);   
  59.         buttons2.setOnClickListener(new OnClickListener() {   
  60.             public void onClick(View v) {   
  61.                 showDialog(DIALOG2);   
  62.             }   
  63.         });   
  64.   
  65.         Button button3 = (Button) findViewById(R.id.button3);   
  66.         button3.setOnClickListener(new OnClickListener() {   
  67.             public void onClick(View v) {   
  68.                 showDialog(DIALOG3);   
  69.             }   
  70.         });   
  71.            
  72.         Button button4 = (Button) findViewById(R.id.button4);   
  73.         button4.setOnClickListener(new OnClickListener() {   
  74.             public void onClick(View v) {   
  75.                 showDialog(DIALOG4);   
  76.             }   
  77.         });   
  78.     }   
  79.   
  80.     private Dialog buildDialog1(Context context) {   
  81.         AlertDialog.Builder builder = new AlertDialog.Builder(context);   
  82.         builder.setIcon(R.drawable.alert_dialog_icon);   
  83.         builder.setTitle(R.string.alert_dialog_two_buttons_title);   
  84.         builder.setPositiveButton(R.string.alert_dialog_ok,   
  85.                 new DialogInterface.OnClickListener() {   
  86.                     public void onClick(DialogInterface dialog, int whichButton) {   
  87.   
  88.                         setTitle("点击了对话框上的确定按钮");   
  89.                     }   
  90.                 });   
  91.         builder.setNegativeButton(R.string.alert_dialog_cancel,   
  92.                 new DialogInterface.OnClickListener() {   
  93.                     public void onClick(DialogInterface dialog, int whichButton) {   
  94.   
  95.                         setTitle("点击了对话框上的取消按钮");   
  96.                     }   
  97.                 });   
  98.         return builder.create();   
  99.   
  100.     }   
  101.     private Dialog buildDialog2(Context context) {   
  102.         AlertDialog.Builder builder = new AlertDialog.Builder(context);   
  103.         builder.setIcon(R.drawable.alert_dialog_icon);   
  104.         builder.setTitle(R.string.alert_dialog_two_buttons_msg);   
  105.         builder.setMessage(R.string.alert_dialog_two_buttons2_msg);   
  106.         builder.setPositiveButton(R.string.alert_dialog_ok,   
  107.                 new DialogInterface.OnClickListener() {   
  108.                     public void onClick(DialogInterface dialog, int whichButton) {   
  109.   
  110.                         setTitle("点击了对话框上的确定按钮");   
  111.                     }   
  112.                 });   
  113.         builder.setNeutralButton(R.string.alert_dialog_something,   
  114.                 new DialogInterface.OnClickListener() {   
  115.                     public void onClick(DialogInterface dialog, int whichButton) {   
  116.   
  117.                         setTitle("点击了对话框上的进入详细按钮");   
  118.                     }   
  119.                 });   
  120.         builder.setNegativeButton(R.string.alert_dialog_cancel,   
  121.                 new DialogInterface.OnClickListener() {   
  122.                     public void onClick(DialogInterface dialog, int whichButton) {   
  123.   
  124.                         setTitle("点击了对话框上的取消按钮");   
  125.                     }   
  126.                 });   
  127.         return builder.create();   
  128.     }   
  129.   
  130.     private Dialog buildDialog3(Context context) {   
  131.         LayoutInflater inflater = LayoutInflater.from(this);   
  132.         final View textEntryView = inflater.inflate(   
  133.                 R.layout.alert_dialog_text_entry, null);   
  134.         AlertDialog.Builder builder = new AlertDialog.Builder(context);   
  135.         builder.setIcon(R.drawable.alert_dialog_icon);   
  136.         builder.setTitle(R.string.alert_dialog_text_entry);   
  137.         builder.setView(textEntryView);   
  138.         builder.setPositiveButton(R.string.alert_dialog_ok,   
  139.                 new DialogInterface.OnClickListener() {   
  140.                     public void onClick(DialogInterface dialog, int whichButton) {   
  141.                         setTitle("点击了对话框上的确定按钮");   
  142.                     }   
  143.                 });   
  144.         builder.setNegativeButton(R.string.alert_dialog_cancel,   
  145.                 new DialogInterface.OnClickListener() {   
  146.                     public void onClick(DialogInterface dialog, int whichButton) {   
  147.                         setTitle("点击了对话框上的取消按钮");   
  148.                     }   
  149.                 });   
  150.         return builder.create();   
  151.     }   
  152.        
  153.     private Dialog buildDialog4(Context context) {   
  154.         ProgressDialog dialog = new ProgressDialog(context);   
  155.         dialog.setTitle("正在下载歌曲");   
  156.         dialog.setMessage("请稍候……");   
  157.         return  dialog;   
  158.     }   
  159. }  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值