在开始此次讲解之前,先弄清几个概念:
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()函数,具体实现代码如下所示:
- private Dialog buildDialog4(Context context){
- ProgressDialog dialog= new ProgressDialog(context);
- dialog.setTitle("正在下载歌曲");
- dialog.setMessage("请稍后......");
- return dialog
- }
private Dialog buildDialog4(Context context){
ProgressDialog dialog= new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍后......");
return dialog
}
下面是具体的详细代码:
- package com.eoeAndroid.dialog;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ActivityMain extends Activity {
- private static final int DIALOG1 = 1;
- private static final int DIALOG2 = 2;
- private static final int DIALOG4 = 4;
- private static final int DIALOG3 = 3;
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG1:
- return buildDialog1(ActivityMain.this);
- case DIALOG2:
- return buildDialog2(ActivityMain.this);
- case DIALOG3:
- return buildDialog3(ActivityMain.this);
- case DIALOG4:
- return buildDialog4(ActivityMain.this);
- }
- return null;
- }
- protected void onPrepareDialog(int id, Dialog dialog){
- if(id==DIALOG1){
- setTitle("测试");
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.alert_dialog);
- Button button1 = (Button) findViewById(R.id.button1);
- button1.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(DIALOG1);
- }
- });
- Button buttons2 = (Button) findViewById(R.id.buttons2);
- buttons2.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(DIALOG2);
- }
- });
- Button button3 = (Button) findViewById(R.id.button3);
- button3.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(DIALOG3);
- }
- });
- Button button4 = (Button) findViewById(R.id.button4);
- button4.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- showDialog(DIALOG4);
- }
- });
- }
- private Dialog buildDialog1(Context context) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setIcon(R.drawable.alert_dialog_icon);
- builder.setTitle(R.string.alert_dialog_two_buttons_title);
- builder.setPositiveButton(R.string.alert_dialog_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的确定按钮");
- }
- });
- builder.setNegativeButton(R.string.alert_dialog_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的取消按钮");
- }
- });
- return builder.create();
- }
- private Dialog buildDialog2(Context context) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setIcon(R.drawable.alert_dialog_icon);
- builder.setTitle(R.string.alert_dialog_two_buttons_msg);
- builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
- builder.setPositiveButton(R.string.alert_dialog_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的确定按钮");
- }
- });
- builder.setNeutralButton(R.string.alert_dialog_something,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的进入详细按钮");
- }
- });
- builder.setNegativeButton(R.string.alert_dialog_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的取消按钮");
- }
- });
- return builder.create();
- }
- private Dialog buildDialog3(Context context) {
- LayoutInflater inflater = LayoutInflater.from(this);
- final View textEntryView = inflater.inflate(
- R.layout.alert_dialog_text_entry, null);
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setIcon(R.drawable.alert_dialog_icon);
- builder.setTitle(R.string.alert_dialog_text_entry);
- builder.setView(textEntryView);
- builder.setPositiveButton(R.string.alert_dialog_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的确定按钮");
- }
- });
- builder.setNegativeButton(R.string.alert_dialog_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setTitle("点击了对话框上的取消按钮");
- }
- });
- return builder.create();
- }
- private Dialog buildDialog4(Context context) {
- ProgressDialog dialog = new ProgressDialog(context);
- dialog.setTitle("正在下载歌曲");
- dialog.setMessage("请稍候……");
- return dialog;
- }
- }