[英]How to prevent dismiss of alert dialog box when user click in background of it?
你好朋友我创建了一个警告对话框,当没有网络连接时,当人们单击“确定”按钮时,他将被定向到“无Internet.class”,它工作正常,但真正的问题是当用户在警告对话框的背景中单击时它会自动关闭。如何停止?? 我的意思是取消对话框。我也使用了此方法SetCancelable(false);。 但它不起作用,它给出了一个错误。
下面是我的代码
AlertDialog.Builder alert = new AlertDialog.Builder(this,R.style.AppCompatAlertDialogStyle);
alert.setTitle("Internet Problem");
alert.setMessage("No Internet!!!");
// alert.SetCancelable(false);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something here where "ok" clicked and then perform intent from activity context
Intent intent = new Intent(BaseActivity.this, NoInternet.class);
BaseActivity.this.startActivity(intent);
}
});
只需添加alert.setCancelable(false);
。 它不起作用的原因(根据代码中的注释)是,您之前使用了错误的方法名称。
AlertDialog.Builder alert = new AlertDialog.Builder(...);
alert.setCancelable(false); // not SetCancelable(false). Case sensitive
});
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(false);
使用setCancelable(false)
方法。 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.