动态更新 AlertDialog 的问题
原文发布于:http://aquarium.yo2.cn/articles/change-the-text-of-alertdialog-in-onpreparedialog.html
在 Android 开发中时常会遇到这样的问题:在删除列表中记录项时弹出确认对话框,对话框的提示消息取决于用户选中的项。例如:Are you sure you want to delete XXX。XXX由用户选中的记录项确定。
通常可以在 Activity 的 onCreateDialog 创建托管对话框(免去对话框状态管理的问题)。在调用 showDialog 的时候,如果该对话框还没有创建,则先调用 onCreateDialog 创建该对话框。随后 onPreparedDialog 被调用。因此,可以在 onPreparedDialog 中更新对话框的内容。
这里有一个诡异的问题:如果 AlertDialog 是在 Activity 的 onCreateDialog 中通过 AlertDialog.Builder 创建的,并且想在 onPreparedDialog 中改变其状态(比如:setTitle, setMessage),你必须先在 onCreateDialog 中调用 AlertDialog.Builder 的方法设置其初始值,否则在 onPreparedDialog 中的方法调用无效。下面通过代码说明此问题
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/btn_show_1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="show dialog 1" /><Button android:id="@+id/btn_show_2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="show dialog 2" /></LinearLayout>
package cn.yo2.aquarium.alertdialogtest;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.os.Bundle;import android.text.format.DateFormat;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Main extends Activity {private static final int TIME_ALERT_DIALOG_1 = 1;private static final int TIME_ALERT_DIALOG_2 = 2;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button1 = (Button) findViewById(R.id.btn_show_1);Button button2 = (Button) findViewById(R.id.btn_show_2);button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubshowDialog(TIME_ALERT_DIALOG_1);}});button2.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubshowDialog(TIME_ALERT_DIALOG_2);}});}@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case TIME_ALERT_DIALOG_1:AlertDialog.Builder timeDialog1 = new AlertDialog.Builder(this);timeDialog1.setTitle("Alert 1: have not called setMessage in onCreateDialog");return timeDialog1.create();case TIME_ALERT_DIALOG_2:AlertDialog.Builder timeDialog2 = new AlertDialog.Builder(this);timeDialog2.setTitle("Alert 2: have called setMessage in onCreateDialog");timeDialog2.setMessage("");return timeDialog2.create();default:break;}return super.onCreateDialog(id);}@Overrideprotected void onPrepareDialog(int id, Dialog dialog) {switch (id) {case TIME_ALERT_DIALOG_1:AlertDialog timeDialog1 = (AlertDialog) dialog;timeDialog1.setMessage(DateFormat.format("h:mm:ss", System.currentTimeMillis()));break;case TIME_ALERT_DIALOG_2:AlertDialog timeDialog2 = (AlertDialog) dialog;timeDialog2.setMessage(DateFormat.format("h:mm:ss", System.currentTimeMillis()));break;default:break;}super.onPrepareDialog(id, dialog);}}