触摸外部时不会关闭AlertDialog

时间:2019-11-19 13:25:11

标签: android android-alertdialog

我试图显示一个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):按预期运行

1 个答案:

答案 0 :(得分:-2)

删除

  

alertDialog.setCanceledOnTouchOutside(true);

保留其他代码并检查。

相关问题