How do I retain an Android alertdialog sometime
I have an AlertDialog in the activity I used, as:
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Unanswered Questions");
ad.setMessage("You have not answered all the questions.");
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.wait(5000);
dosometing();
}
});
ad.show();
I want click OK, it does not disappear. I click OK,开发者_JAVA技巧and do dosometing()
,then it disappear. I meet question, when i click OK,the dialog disappear. I know I can use activity and theme deal with this problem. if I can use the AlertDialog,
Thank you.
what is it that you are waiting on before removing your dialog? If it is some sort of processing task, put it all in its own method that returns a boolean and call that method just before you call dialog.cancel(). Like so:
...
public void onClick(DialogInterface dialog, int id) {
boolean closeNow = dosometing();
if (closeNow) {
dialog.cancel();
}
}
...
that should work. The program should wait for dosomething() to return before continuing. If not try:
...
public void onClick(DialogInterface dialog, int id) {
recursiveMethod(dialog, id)
}
...
private void recursiveMethod(DialogInterface dialog, int id) {
boolean closeNow = dosometing();
if (closeNow) {
dialog.cancel();
} else {
recursiveMethod(dialog, id);
}
}
Edit: I'd love to know if the second one works. It might not be able to cancel the dialog, but I've never tried doing recursion like that in java before. If you give it a shot, let me know if it throws up an error.
Edit #2, that will actually spawn a bunch of calls to doSomething(). . . not the ideal situation.
精彩评论