首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >事件打开/关闭popupWindow

事件打开/关闭popupWindow
EN

Stack Overflow用户
提问于 2015-01-08 14:40:30
回答 2查看 3.6K关注 0票数 2

我正在尝试创建一种具有popupMenuItem和自定义布局的popupWindow。

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

但是现在它不起作用了,我的setTouchInterceptor还没有触发,你有办法解决这个问题吗?

每次打开这个popupWindow,我都无法访问所有其他UI元素,即使使用setOutsideTouchable(true)。

这里我的代码:

  • popupMenuImageView是我的下拉按钮(实际上是图像视图),它显示了我的弹出窗口
  • popupWindow我的PopupWindod对象的全局变量。 ( getView().findViewById(R.id.popup_menu_imageView);= (ImageView) popupMenuImageView =(ImageView)popupMenuImageView.setOnClickListener(新View.OnClickListener() ){@重写公共无效onClick(View )){ if (popupWindow == null) { int宽度= 375;int高度= 240;tableLayout TableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);LayoutInflater = ( layoutInflater ) LayoutInflater视图布局=LayoutInflater tableLayout;popupWindow =新PopupWindow(布局、宽度、高度、真);popupWindow.setOutsideTouchable(真);popupWindow.setFocusable(真);新的if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ (){@View.OnTouchListener(@重写公用布尔值onTouch(查看v,MotionEvent事件)){MotionEvent popupWindow.dismiss();返回真;}返回false;);} popupWindow.showAsDropDown(popupMenuImageView);} });

另外,我也尝试过这样做,但没有奏效:

代码语言:javascript
复制
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();
            }
        }
    };

私有空createMenuItem(){ 如果( getView().findViewById(R.id.custom_popup_menu_id);== null) { int宽度= 375;int高度= 240;tableLayout TableLayout = (TableLayout) == layoutInflater LayoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = layoutInflater.inflate(R.layout.custom_popup_menu,tableLayout);popupWindow =新PopupWindow(布局、宽度、高度、真);popupWindow.setOutsideTouchable(假);popupWindow.setFocusable(真);}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-08 15:01:58

如果您只想通过单击弹出窗口外部关闭弹出窗口,则不需要设置拦截器。只需将背景可绘图设置为空,例如

代码语言:javascript
复制
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popupWindow.setOutsideTouchable(true);

并删除:

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2015-01-08 15:01:33

我相信您需要在显示弹出窗口之前先使用setBackgroundDrawable()才能处理触摸事件:

代码语言:javascript
复制
popupWindow.setBackgroundDrawable(new ColorDrawable());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27842673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档