【问题标题】:Don't dismiss PopupWindow when clicking outside, only when the Close button is clicked在外部单击时不要关闭 PopupWindow,仅在单击关闭按钮时
【发布时间】:2014-09-16 02:40:28
【问题描述】:

我有一个带有关闭按钮的 PopupWindow,我只想在单击此关闭按钮时关闭 PopupWindow。另外,我不希望底层的 Activity-View 受到任何触摸的影响。

以前有人问过这个问题:Android Popup Window dismisses when clicked outside。但由于这是三年前(2011 年)的问答,我想知道现在是否有更好的解决方案,或者我确实应该使用接受的答案的方法。

【问题讨论】:

  • 我也遇到了这个问题。

标签: android android-layout popup popupwindow dismiss


【解决方案1】:

你需要同时做:

    popupWindow.setOutsideTouchable(false);
    popupWindow.setFocusable(false);

签出this

【讨论】:

    【解决方案2】:

    试试这个link 或以下代码之一

    pw.setOutsideTouchable(false);
    

    @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);
    }
    

    希望这会有所帮助...

    【讨论】:

    • 链接:这与我想要的相反,因为他想在点击外部时关闭。 setOutsideTouchable(false):这有助于忽略我的 Activity 视图的 onClicks / onTouches,但它仍然“关闭”(设置为背景而无法再访问它)PopupWindow。关于最后一个:当我在 PopupWindow (创建和显示此 PopupWindow 的同一活动)外部单击时如何完成 Activity 帮助我。 :S 澄清一下:在弹出窗口之外单击时,我什么都不想做(忽略所有触摸事件并仍然显示 PopupWindow)。
    • final PopupWindow popUpReject = new PopupWindow(popuplayoutReject, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT, false); popUpReject.setOutsideTouchable(false); 试试这个代码..
    • 你试过这个pw.setOutsideTouchable(false); 吗?这就像你想要的一样简单。它将关闭所有对弹出对话框的屏幕触摸,并将弹出关闭设置为单击关闭按钮。
    • 是的,我做到了。但是,即使使用popup.setOutsideTouchable(false),PopupWindow 仍然会消失,当我在 PopupWindow 外部单击时,我仍然会收到InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.-Warning。
    猜你喜欢
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    相关资源
    最近更新 更多