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

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

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

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

最佳答案

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

关于android - 在弹出窗口外单击时,如何关闭弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49483491/

相关文章:

android - DialogFragment 中的 getViewLifecycleOwner() 导致崩溃

安卓 : GridView was Not Marked insit of that it load to empty Activity

android - 在android中添加水印图片

android - 如何使对象位于 ScrollView 的底部

android - MotionLayout 是否支持 fragment 之间的共享元素转换?

java - 膨胀 Facebook 登录按钮时出错。

android - FragmentManager 添加相同 Fragment 类型的多个实例

android - Eclipse Android 模拟器不会启动

android - RecyclerView 中的 ViewPager 作为行项目

android - 设置 TextView 的背景仅填充我的文本 - Java