我有一个正在使用主题的活动。对话框样式,使其成为另一个活动上方的浮动窗口。但是,当我在对话框窗口外(在后台活动上)单击时,对话框关闭。我怎样才能阻止这种行为?
当前回答
警告对话框已弃用,因此使用对话框对话框=新对话框(此);
防止外界接触
dialog.setCanceledOnTouchOutside(false);
其他回答
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
我想这对你有帮助。这对我很管用
这对你有帮助。它是处理touch outside事件的一种方式:
如何取消一个对话主题,如活动时触摸窗外?
通过捕捉事件,什么都不做,我认为你可以阻止关闭。但奇怪的是,你的活动对话框的默认行为应该是当你触摸外部时不关闭自己。
(PS:代码使用WindowManager.LayoutParams)
你实际上有一个活动(即使它看起来像一个对话框),因此你应该调用setFinishOnTouchOutside(false)从你的活动,如果你想保持它打开时,后台活动被单击。
编辑:这只适用于android API级别11或更高
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
在jetpack组合中,使用遣散onclickoutside = false属性来防止关闭。
AlertDialog(
title = {
Text("Title")
},
text = {
Text(text = name)
},
onDismissRequest = onDismiss,
confirmButton = {
TextButton(onClick = onDismiss ) {
Text("Yes")
}
},
dismissButton = {
TextButton(onClick = onDismiss ) {
Text("Cancel")
}
},
properties = DialogProperties(
dismissOnClickOutside = false
)
)
}
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 更改单选按钮的圆圈颜色
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?