【问题标题】:AlertDialog not dismissing when touch outside触摸外部时,AlertDialog 不会关闭
【发布时间】: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 样式
  • 使用这个链接帮你stackoverflow.com/a/13526778/7779712
  • 我添加了样式,但只是一些颜色
  • answer 阻止对话框关闭,我想保持启用该行为。
  • @IgniteCoders 保持冷静并尝试更改您正在运行应用程序的设备

标签: android android-alertdialog


【解决方案1】:

删除

alertDialog.setCanceledOnTouchOutside(true);

保持其他代码不变并检查。

【讨论】:

    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多