AlertDialog 是可跳过的。如何避免这种情况?

AlertDialog is skippable. How avoid this?

我已经开始创建一个 android 应用程序(为我自己,而不是为发布),但我有一些我无法解决的小问题:当我显示 AlertDialog 时,如果我触摸指出它是 window(即灰色区域),AlertDialog 没有做任何事情就消失了。

是否可以让 AlertDialog 不可跳过?或者当它消失时执行 "cancel" 按钮的代码。 有一个屏幕:http://i.imgur.com/9c6SFK2.png 谢谢。

您可以将 cancelable false 设置为 allertdialog

    DialogFragment dialog= 
     MyAlertDialogFragment.newInstance( R.string.alert_dialog_two_buttons_title);

     dialog.setCancelable(false);

试试这个:

AlertDialog.Builder ad;

显示警报对话框的位置:

ad=new AlertDialog.Builder(MainActivity.this);
                final AlertDialog ab=ad.create();
                ad.setCancelable(false);
                ad.setMessage("YOUR_MESSAGE");
                ad.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ab.cancel();
                    }
                });
                ad.show();

查看此文档 Android Developer | setCanceledOnTouchOutside

您可以使用此方法设置对话框是否可通过外部触摸取消

dialog.setCanceledOnTouchOutside(false);