【发布时间】:2019-11-19 13:25:11
【问题描述】:
我正在尝试显示一个AlertDialog,它可以通过后退按钮或在对话框外触摸来解除。那里有很多解决方案,我知道哪个是正确的,因为我之前使用过它。但现在我无法实现。这是我的代码:
AlertDialog alertDialog = new AlertDialog.Builder(activity, R.style.AppTheme_Dark_Dialog).create();
//Some setup of the dialog...
...
//This works when I press the back button
alertDialog.setCancelable(true);
//This should be the right answer of my problem
alertDialog.setCanceledOnTouchOutside(true);
alertDialog.show();
据我所知,使用真正的参数值调用此方法就足够了:
alertDialog.setCanceledOnTouchOutside(true);
但不会工作。谁能赐教!?
编辑:一些测试
正如Ugas 所说,我更换了设备进行测试,它工作正常。
- Android 5.1 (API 22):不工作
- Android 9 (API 28):按预期工作
【问题讨论】:
-
我们可以看看你的 AppTheme_Dark_Dialog 样式
-
我添加了样式,但只是一些颜色
-
answer 阻止对话框关闭,我想保持启用该行为。
-
@IgniteCoders 保持冷静并尝试更改您正在运行应用程序的设备
标签: android android-alertdialog