开发者

AlertDialog with Timeout

I want to create an AlertDialog with a timer in it. Basically开发者_JAVA技巧 I want the dialog to disappear if the user doesn't make a decision on the buttons that are in the dialog after 30 seconds. I can create a dialog using an AlertDialog builder, but it seems like I cannot update the text using the .setMessage() method after the .show() method has been executed.

Any ideas on what I should do?

So in the code below if I run the .setMessage() method after the .show() method I see nothing. This is telling me that I cannot update the text in the dialog box in real time.

// 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", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    alert = builder.create();
    alert.setMessage("test");
    alert.show();


I think you need to have previously called setMessage() at least once prior to calling show() in order to be able to call it again afterwards. You can set it to an empty value or to a temporary one, then update it later:

alert.setMessage("test");
alert.show();
...
alert.setMessage("test again");


I'd recommend inflating a custom Dialog from XML so that you can get a handle to the TextView (or whatever) that you want to change in your code. It's really easy:

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog


You can do it with 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>"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜