繁体   English   中英

在弹出窗口外单击时,如何关闭弹出窗口?

[英]How do I enable a popup to close when clicked outside of it?

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

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

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

使您的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);

你试过setCanceledOnTouchOutside吗?

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

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

让弹出窗口 setTouchable 为 true,setTouchInterceptor 和 get 返回 false,然后您可以在弹出窗口外单击将其关闭。

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

如果它不能工作,请告诉我,我会看到我想念的。

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

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

2023 年更新:现已更改为setCancelable

dialog.setCancelable(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM