我有一个正在使用主题的活动。对话框样式,使其成为另一个活动上方的浮动窗口。但是,当我在对话框窗口外(在后台活动上)单击时,对话框关闭。我怎样才能阻止这种行为?
当前回答
将对话框可取消设置为false就足够了,并且您可以在警报对话框外触摸或单击后退按钮将使警报对话框消失。所以就用这个吧:
setCancelable(假)
另一个函数不再需要了: dialog.setCanceledOnTouchOutside(假);
如果你正在创建一个临时对话框,并想把这行代码放在那里,下面是一个例子:
new AlertDialog.Builder(this)
.setTitle("Trial Version")
.setCancelable(false)
.setMessage("You are using trial version!")
.setIcon(R.drawable.time_left)
.setPositiveButton(android.R.string.yes, null).show();
其他回答
使用这个代码,它为我工作
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setCancelable(false);
将对话框可取消设置为false就足够了,并且您可以在警报对话框外触摸或单击后退按钮将使警报对话框消失。所以就用这个吧:
setCancelable(假)
另一个函数不再需要了: dialog.setCanceledOnTouchOutside(假);
如果你正在创建一个临时对话框,并想把这行代码放在那里,下面是一个例子:
new AlertDialog.Builder(this)
.setTitle("Trial Version")
.setCancelable(false)
.setMessage("You are using trial version!")
.setIcon(R.drawable.time_left)
.setPositiveButton(android.R.string.yes, null).show();
我在onCreate()中使用这个,似乎可以在任何版本的Android上工作;在5.0和4.4上测试。x,不能在姜饼上测试,三星设备(Note 1运行GB)默认是这样的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
setFinishOnTouchOutside(false);
}
else
{
getWindow().clearFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
}
super.onCreate(savedInstanceState);
以下是我的解决方案:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setCancelable(false);
在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的主要区别是什么?