Android Don';t单击按钮后关闭AlertDialog

Android Don';t单击按钮后关闭AlertDialog,android,android-alertdialog,Android,Android Alertdialog,我需要帮助 我正在开发一个android应用程序,遇到了一个问题。 我有一个警报对话框,其中包含两个按钮(正按钮和负按钮)。单击某个按钮时,某些代码将运行,然后对话框将关闭 dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {

我需要帮助

我正在开发一个android应用程序,遇到了一个问题。 我有一个
警报对话框
,其中包含两个按钮(正按钮和负按钮)。单击某个按钮时,某些代码将运行,然后对话框将关闭

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // some code
        }
    });
但那不是我想要的。当用户单击否定按钮时,我希望运行一些代码,然后对话框不应该关闭

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // some code
            // code to prevent the dialog from being closed ?
        }
    });
我可以做些什么来防止在单击“正”或“负”按钮时关闭对话框

我尝试使用以下代码:

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
但它不起作用,因为现在用户无法单击按钮

顺便说一句,我开发的sdk最低版本为16


谢谢你的帮助

如果onClick中的代码包含dialog.dismise(),则只有dialog会关闭

谢谢您的建议。我制作了一个自定义对话框,现在它可以像我想要的那样工作。

创建您自己的自定义对话框您的一些代码是否也包含
dialog.dismise()
?可能重复
   String Strmessage="message";
            final AlertDialog.Builder alt_bld = new AlertDialog.Builder(SplashActivity.this);
            alt_bld.setMessage(Strmessage);
            alt_bld.setCancelable(true);
            alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            })
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            AlertDialog alert = alt_bld.create();           
            alt_bld.setMessage(Strmessage);
            alert.show();

        }