Tap outside Android dialog to dismiss it?(点击 Android 对话框外部以将其关闭?)
问题描述
我想知道是否有可能以某种方式在弹出对话框(或具有对话框主题的 Activity)外部点击,然后通过点击它外部将其关闭?
I was wondering if it's possible to somehow tap outside a popup dialog (or an Activity with a dialog theme), and dismiss it by just tapping outside of it?
我做了一张快速的图片来说明它:
I made a quick picture to illustrate it:
通常情况下,您必须按返回键才能关闭对话框,但在 Honeycomb 上,由于所有屏幕空间,选择只在对话框外点击可能会很棒.
Normally, you have to press the back key to dismiss the dialogs, but on Honeycomb it could be great to have the option of just tapping outside the dialog, due to all the screen estate.
推荐答案
我的应用是一个带有 Theme.Holo.Dialog 的单一活动.就我而言,另一个答案不起作用.它只让其他后台应用程序或启动屏幕接收触摸事件.
My app is a single activity with Theme.Holo.Dialog. In my case the other answer did not work. It only made the other background apps or the launch screen to receive touch events.
我发现使用 dispatchTouchEvent 在我的情况下有效.我认为这也是一个更简单的解决方案.下面是一些示例代码,说明如何使用它来检测带有 Dialog 主题的 Activity 之外的点击:
I found that using dispatchTouchEvent works in my case. I think it is also a simpler solution. Here's some sample code on how to use it to detect taps outside the activity with a Dialog theme:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// Tapped outside so we finish the activity
this.finish();
}
return super.dispatchTouchEvent(ev);
}
这篇关于点击 Android 对话框外部以将其关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:点击 Android 对话框外部以将其关闭?


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01