事件打开/关闭popupWindow

时间:2015-01-08 14:40:31

标签: android events popupwindow

我尝试使用popupWindow和自定义布局创建一种popupMenuItem。

我有一个按钮,当我点击它时会显示popupWindow。当我再次单击此按钮或单击popupWindow外部时,我想触发和事件来关闭此popupWindow。

但目前它还没有工作,我的setTouchInterceptor没有被触发,你有想法解决这个问题吗?

每次弹出窗口打开时,即使使用setOutsideTouchable(true),我也无法访问所有其他UI元素。

这是我的代码:

  • popupMenuImageView是我的下拉按钮(实际上是imageview),它显示了我的弹出窗口
  • popupWindow我的PopupWindod对象的全局变量。

    popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
    
    popupMenuImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            if (popupWindow == null) {
                int width = 375;
                int height = 240;
    
                TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
                LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);
    
                popupWindow = new PopupWindow(layout, width, height, true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                            popupWindow.dismiss();
                            return true;
                        }
                        return false;
                    }
                });
    
            }
            popupWindow.showAsDropDown(popupMenuImageView);
        }
    });
    

我也试过这个也不行的:

popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);

View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (isShowing) {
                popupWindow.showAsDropDown(popupMenuImageView);
                isShowing = false;
            }else{
                popupWindow.dismiss();
            }
        }
    };
  

private void createMenuItem(){

        if (popupWindow == null) {
            int width = 375;
            int height = 240;

            TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
            LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);

            popupWindow = new PopupWindow(layout, width, height, true);
            popupWindow.setOutsideTouchable(false);
            popupWindow.setFocusable(true);
        }

2 个答案:

答案 0 :(得分:2)

如果您只是想通过点击外部来关闭弹出窗口,则不需要设置拦截器。只需将背景drawable设置为null,例如

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

并删除:

popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

请在PopupWindow - Dismiss when clicked outside

检查我对类似问题的回答

答案 1 :(得分:1)

我认为在显示弹出窗口之前你需要setBackgroundDrawable()才能处理触摸事件:

popupWindow.setBackgroundDrawable(new ColorDrawable());