我有一个正在使用主题的活动。对话框样式,使其成为另一个活动上方的浮动窗口。但是,当我在对话框窗口外(在后台活动上)单击时,对话框关闭。我怎样才能阻止这种行为?


当前回答

当在onCreate中使用dialog作为一个活动时,添加这个

setFinishOnTouchOutside(false);

其他回答

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true); 
//use this to dismiss the dialog on outside click of dialog

dialog.setCanceledOnTouchOutside(false);
//use this for not to dismiss the dialog on outside click of dialog.

观看这个链接了解更多关于对话的细节。

dialog.setCancelable(false);
//used to prevent the dismiss of dialog on backpress of that activity

dialog.setCancelable(true);
//used to dismiss the dialog on onbackpressed of that activity

builder.setCancelable(假);


Mensaje(视图v){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("¿Quieres ir a el Menú principal?");
    builder.setMessage("Al presionar SI iras a el menú y saldras de la materia.");
    builder.setPositiveButton("SI", null);
    builder.setNegativeButton("NO", null);
    builder.setCancelable(false);
    builder.show();
}
        alert.setCancelable(false);
        alert.setCanceledOnTouchOutside(false);

我想这对你有帮助。这对我很管用

对于更高的API 10,对话框在外部触摸时消失,而在低于API 11的情况下,对话框不会消失。为了防止这种情况,你需要做到:

xml: <项目名称=“android: windowcloseonteutside”>false</项目>

OR

在onCreate()方法中,使用:this.setFinishOnTouchOutside(false);

注:对于API 10及以下,此方法无效,也不需要。

使用这个代码,它为我工作

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
 alertDialog.setCancelable(false);