带有超时的AlertDialog

问题描述

| 我想创建一个带有计时器的AlertDialog。基本上,如果用户在30秒后不对对话框中的按钮做出决定,我希望对话框消失。我可以使用AlertDialog构建器创建对话框,但是似乎在执行.show()方法之后,无法使用.setMessage()方法更新文本。 关于我应该做什么的任何想法? 因此,在下面的代码中,如果我在.show()方法之后运行.setMessage()方法,我什么也看不到。这说明我无法实时更新对话框中的文本。
// Create the alert dialog with a alert builder.
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(\"Confirm Settings Change\")
           .setCancelable(false)
           .setPositiveButton(\"Confirm\",new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog,int id) {
                   this.finish();
               }
           })
           .setNegativeButton(\"Cancel\",int id) {
                    dialog.cancel();
               }
           });
    alert = builder.create();
    alert.setMessage(\"test\");
    alert.show();
    

解决方法

我认为您必须先调用
setMessage()
之前先调用
show()
,然后才能再次调用它。您可以将其设置为空值或临时值,然后稍后进行更新:
alert.setMessage(\"test\");
alert.show();
...
alert.setMessage(\"test again\");
    ,我建议从XML扩展自定义对话框,以便您可以获取要在代码中更改的TextView(或其他内容)的句柄。这真的很简单: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog     ,您可以使用ProgressDialog来做到这一点:
public Context ioContext;
private ProgressDialog ioProgressDialog;


ioProgressDialog = ProgressDialog.show(ioContext,\"\",\"This is my dialog!\",true);
ioProgressDialog.setMessage(Html.fromHtml(\"<font color=\'black\'>\" + isMensaje + \"</font>\"));