点击Android外的对话框可以关闭它? [英] Tap outside Android dialog to dismiss it?

查看:587
本文介绍了点击Android外的对话框可以关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以以某种方式点击弹出对话框(或具有对话主题的活动),然后通过轻轻点击它来解除它。

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主题检测活动外的水龙头的示例代码:

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外的对话框可以关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆