要取消上一个对话框并显示下一个对话框,可以使用以下步骤:
private AlertDialog dialog; private Boolean isDialogShowing = false;
if (dialog != null && dialog.isShowing()) { isDialogShowing = true; dialog.dismiss(); }
if (isDialogShowing) { isDialogShowing = false; } else { //显示下一个对话框 }
下面是完整的示例代码:
private AlertDialog dialog; private Boolean isDialogShowing = false;
private void showDialog() { if (dialog != null && dialog.isShowing()) { isDialogShowing = true; dialog.dismiss(); }
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog");
builder.setMessage("This is a dialog.");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog = builder.create();
dialog.show();
if (isDialogShowing) {
isDialogShowing = false;
} else {
//显示下一个对话框
}
}