多个对话框单击即可弹出

时间:2013-07-26 04:05:40

标签: android dialog android-alertdialog

我有以下代码来生成对话框。流程就像这样

测试结束 - >用户选择保存或删除 - >如果用户选择删除,则会弹出另一个对话框,要求删除确认。

程序运行正常。但是偶尔会弹出超过1个删除对话框。我在某个地方犯了错误吗?

感谢。

private void buildEndTestDialog(){

    alert = new AlertDialog.Builder(getActivity());
    alert.setTitle("End of Test");
    alert.setCancelable(false);


    alert.setNegativeButton("Save", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Save
            alert.dismiss();
        }
    });

    alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            buildDeleteDialog();
        }
    });

    alert.show(); 
}

private void buildDeleteDialog() {

    alert = new AlertDialog.Builder(getActivity());
    alert.setTitle("Delete");
    alert.setCancelable(false);
    alert.setMessage("Are you sure you want to delete this result?");
    alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //delete result
        }
    });

    alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            dialog.dismiss();
        }
    });

    alert.show(); 
}

1 个答案:

答案 0 :(得分:0)

尝试..

    private void buildEndTestDialog(){
               ///rest of code
        alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                buildDeleteDialog();
            }
        });

        alert.show(); 
    }

    private void buildDeleteDialog() {

        ///rest of code

        alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                dialog.dismiss();
               buildEndTestDialog();
            }
        });

        alert.show(); 
    }