【问题标题】:Dimiss popup when touch outside触摸外部时关闭弹出窗口
【发布时间】:2014-02-08 06:11:08
【问题描述】:

我想在触摸外面时关闭PopupWindow,我从这个 SO question 中得到了答案。

here is that Link

这里他们要求包含这两行代码。

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
myPopupWindow.setOutsideTouchable(true);

现在,当我触摸PopupWindow 之外时,弹出窗口就会消失。

setOutsideTouchable(true); 单独不起作用。当我设置背景 Drawable 时,它​​正在工作。 这个魔法是怎么发生的?谁能解释一下?

new BitmapDrawable() 也已弃用。有什么选择吗?

【问题讨论】:

  • 因为new BitmapDrawable() 已弃用,您可以使用ColorDrawablemyPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  • 是的,它正在工作。当我设置 backgrounddrawable 时,你能告诉我弹出窗口是如何消失的吗?
  • 对不起...我不知道...

标签: android popupwindow


【解决方案1】:

试试下面的代码:

myPopupWindow.setCanceledOnTouchOutside(true);
myPopupWindow.setCancelable(true);

【讨论】:

  • 我使用的是 PopupWindow 而不是 Dialog。两种方法都未定义 PopupWindow。
  • 我创建了popupWindow 如下:PopupWindow popup = new PopupWindow(mContext); popup.setContentView(layoutPopup); popup.setWidth(850); popup.setHeight(550); popup.setFocusable(true); // Clear the default translucent background popup.setBackgroundDrawable(view.getResources().getDrawable( R.drawable.white_color_patch)); 这对我来说非常有效。
  • 所以,替代方案是popUp.setFocusable(true);
  • popUp.setFocusable(true);仅适用于 setBackgroundDrawable。但我不知道当我设置 backgroundDrawable 时会发生什么魔法。
【解决方案2】:

使用 TouchInterceptor 关闭弹出窗口:-

private LayoutInflater inflater;
private PopupWindow pw;
private View popupView;

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.popup_layout, null, true);
pw = new PopupWindow(popupView,750,500,true);
pw.setBackgroundDrawable(new BitmapDrawable());

pw.setTouchInterceptor(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                    pw.dismiss();

                    return true;
                }
        return false;
    }
});

pw.showAtLocation(findViewById(R.id.main_layout),Gravity.BOTTOM, 3, 35);

【讨论】:

  • k.你能发布你的弹出窗口的完整代码吗?
  • final PopupWindow addToOrderDialog = new PopupWindow(layout,android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT); addToOrderDialog.setContentView(布局); addToOrderDialog.setBackgroundDrawable(new BitmapDrawable()); addToOrderDialog.setOutsideTouchable(true);
  • @saravanan 看到我编辑的答案,这也不起作用。恢复原状。
【解决方案3】:

使您的弹出窗口采用全屏透明“根”可点击布局,然后在此处添加带有尺寸的弹出窗口 (popup.setWidth(850); popup.setHeight(550))。还要使根透明布局可点击,这样您就会知道用户触摸“外部”因为用户唯一的弹出窗口将是可见的。这是根布局:

final LinearLayout contentLayout = new LinearLayout(activity);
contentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
contentLayout.setOrientation(LinearLayout.VERTICAL);
contentLayout.setClickable(true);
contentLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dismissPopup();
    }
});

现在您只需将弹出窗口添加到该布局中。为什么这种方式更好?因为你可以设置 contentLayout 背景颜色来达到使整个屏幕被弹出窗口遮蔽的效果,这样用户就可以看到只有弹出窗口是模态的并且现在是活动的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2019-11-10
    相关资源
    最近更新 更多