問題描述
我想知道是否有可能以某種方式在彈出對話框(或具有對話框主題的 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 對話框外部以將其關閉?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!