【问题标题】:How do I enable a popup to close when clicked outside of it?如何在弹出窗口外部单击时关闭它?
【发布时间】:2018-03-26 03:24:01
【问题描述】:

我正在尝试制作一个弹出窗口,其中将包含文本字段和信息以询问用户,但我想知道如何制作它以便用户可以通过单击主要片段/活动所在的弹出窗口外部来关闭它是。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tabstudygroups, container, false);

        listview = (ListView) rootView.findViewById(R.id.clist2);
        addCourseButton = (Button) rootView.findViewById(R.id.caddcoursebutton);

        // do stuff here

        addCourseButton.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        if(v == addCourseButton) {
            View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
            final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

            // HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);

            Button btn = (Button) popupView.findViewById(R.id.button);

            popupWindow.showAsDropDown(popupView, 0, 0);
        }
    }
}

【问题讨论】:

标签: android android-layout android-fragments android-popupwindow


【解决方案1】:

将您的 PopupWindow 设置为 wrap_content 并使其具有焦点。

final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

            // HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


            Button btn = (Button) popupView.findViewById(R.id.button);

            popupWindow.showAsDropDown(popupView, 0, 0);

【讨论】:

  • 这有效,但弹出窗口现在没有居中
  • @SQLUser 添加你的 XML 我可以帮你解决这个问题
【解决方案2】:

通常您会使用对话框和 OnCancelListener 来执行此操作。如果你想要弹出的灵活性,你可以通过将它设置在可触摸之外,然后调用 setTouchInterceptor 来拦截触摸来获得相同的东西。如果触摸在窗口内,请记住返回 false,因此它将沿着触摸链向下移动到实际视图。

【讨论】:

    【解决方案3】:

    确保popupWindow.showAsDropDown(popupView, 0, 0); 在这些之后 popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    【讨论】:

    • popupWindow.setOutsideTouchable(true);它帮助我在对话框的点击侧关闭弹出窗口。谢谢。
    【解决方案4】:

    你试过setCanceledOnTouchOutside吗?

    【讨论】:

    • 我该怎么做呢?
    • Dialog dialog = new Dialog(context) dialog.setCanceledOnTouchOutside(true);
    【解决方案5】:

    当用户触摸弹出窗口之外时,您可以使用 setCanceledOnTouchOutside(true) 关闭弹出窗口。

    Dialog dialog = new Dialog(context)
    dialog.setCanceledOnTouchOutside(true);
    

    【讨论】:

      【解决方案6】:

      让弹窗setTouchable为true,setTouchInterceptor返回false,然后在弹窗外点击即可关闭。

      popWindow.setTouchable(true);
      popWindow.setTouchInterceptor(new View.OnTouchListener() {
          @SuppressLint("ClickableViewAccessibility")
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              return false;
          }
      });
      

      如果它不起作用,请告诉我,我会看到我错过了什么。

      这个答案类似于 Gabe Sechan 的答案,只是在发布这个答案之前我没有发现......

      【讨论】:

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