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


当前回答

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

其他回答

我也面临着同样的问题。为了处理它,我设置了一个对话框的OntouchListener,在里面什么都不做。但旋转屏幕时对话框也会消失。为了解决这个问题,我设置了一个变量来告诉我对话框是否正常解散。然后我设置了一个OnDismissListener到我的对话框,在里面我检查变量。如果对话框正常解散,我什么都不做,否则我再次运行对话框(并将他的状态设置为在我的情况下解散时)。

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();
}

对我来说有用的是创建DialogFragment并将其设置为不可取消:

dialog.setCancelable(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

以下是我的解决方案:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setCancelable(false);