java - 另一个警报对话框之后的警报对话框?第一个不见了!安卓

标签 java android dialog progressdialog android-alertdialog

我有一个 Activity (ImportActivity),用户在其中输入一些度量值并将它们保存到 sqlite 数据库中。

当用户在导入到数据库后单击保存按钮时,我会出现一个警告对话框 (SAVEORBACK_DIALOG_ID),用户可以在其中离开此 Activity 或导入另一个度量。它工作得很好。

我的问题是当我尝试在 (SAVEORBACK_DIALOG_ID) 警报对话框之前显示另一个警报对话框 (SMS_DIALOG_ID) 时。这是因为我想询问用户是否发送短信。

当我运行它时,它只显示第二个警告对话框(SAVEORBACK_DIALOG_ID)!!

ImportActivity this is final dialog

我在 Activity 中有:

static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
static final int SAVEORBACK_DIALOG_ID = 2;
static final int SMS_DIALOG_ID = 3;

我从我的 Activity 中调用它们:

// sms dialog (send sms to doctor? yes/no)
showDialog(SMS_DIALOG_ID);

// save or back dialog
showDialog(SAVEORBACK_DIALOG_ID);

这是我的对话框的 onCreateDialog 方法(我删除了一些以便于阅读):

    @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
                false);
    case SAVEORBACK_DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Information saved successfully ! Add Another Info?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                ImportActivity.this.finish();

                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // get the new date
                                // Clearing the fields & update date/time
                                // textviews
                            }
                        });
        AlertDialog dialog = builder.create();
        return dialog;

        // case sms dialog
    case SMS_DIALOG_ID:
        AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setMessage("High blood pressure ! Send sms to doctor?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // do nothing - just continue
                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // try to send sms - report status
                            }
                        });
        AlertDialog dialog2 = builder2.create();
        return dialog2;
        //

    }
    return null;
}

最佳答案

这些命令是连续执行的,如您所见,它们会覆盖第一个对话框:

    // sms dialog(send sms to doctor?yes/no)
    showDialog(SMS_DIALOG_ID);

    // save or back dialog
    showDialog(SAVEORBACK_DIALOG_ID);

因为 showDialog() 在显示第二个对话框之前不会等待第一个对话框的响应。

只需将第二个 showDialog() 命令移动到第一个对话框中的按钮即可:

.setPositiveButton("No",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            // do nothing - just continue
            ImportActivity.this.showDialog(SAVEORBACK_DIALOG_ID);
        }
    })
// etc, etc

或者,您可以使用 OnDismissDialogListener,每当对话框关闭时(通过单击按钮、取消操作等)调用它:

case SMS_DIALOG_ID:
    ...
    AlertDialog dialog2 = builder2.create();

    dialog2.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            ImportActivity.this.showDialog(SAVEORBACK_DIALOG_ID);
        }
    });

    return dialog2;
}

关于java - 另一个警报对话框之后的警报对话框?第一个不见了!安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501357/

相关文章:

java - andEngine:为什么触摸事件不起作用?

java - 主线程上的 fatal error

java - 如果用户在警报对话框中按"is"按钮,如何在列表中添加项目,而如果按“否”则不添加该项目

java - Servlet 中的安全图像上传

android - 在 android studio 中如何从磁盘重新加载代码文件?

linux - Lazarus:项目引发异常类 "Ereaderror",并显示消息:未知属性 "Caption"

linux - 在对话框实用程序中使用 --output-fd 参数

Java JUNG - 不兼容的类型

java - java 和 Scala 中非 Ascii 字符的子字符串

java - 具有有限选项的自定义数据类型,在 Java 中