【问题标题】:PopupWindow TouchInterceptor not workingPopupWindow TouchInterceptor 不工作
【发布时间】:2013-03-22 12:20:32
【问题描述】:

我正在尝试测试 PopupWindow 类。我创建了这个方法来显示弹出窗口:

    public void showPopup(){
            LayoutInflater layoutInflater   = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popup = new PopupWindow(popupView, 
                       LayoutParams.WRAP_CONTENT,  
                         LayoutParams.WRAP_CONTENT);  
            popup.setOutsideTouchable(true);
            popup.setTouchable(true);
            popup.setTouchInterceptor(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.d("POPUP", event.toString());
                    if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
                        popup.dismiss();
                        return true;
                    }
                    return true;
                }
            });
            popup.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 200);
}

弹出窗口显示正确,顺便说一句,触摸拦截器似乎根本不起作用:我没有得到任何日志信息,当然如果在它外面按下弹出窗口也不会消失。

我必须在弹出窗口或托管它的 Activity 中设置其他属性吗?

【问题讨论】:

  • 我遇到了同样的问题,让我记住我是如何解决的。

标签: android popupwindow ontouchlistener


【解决方案1】:
 pw.setBackgroundDrawable (new BitmapDrawable());
 pw.setFocusable(false);
 pw.setOutsideTouchable(true); 

使用此代码希望这会有所帮助

【讨论】:

  • 嗨,我一直在研究一段代码,它在弹出窗口中有列表。所以我希望它是可聚焦的。如何处理任何外部触摸事件以关闭此弹出窗口。在我的下一条评论中,我将粘贴我的方法
  • popupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { popupWindow.dismiss(); if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { Log.e(TAG, "一些事件发生在窗口外:外部动作"); return true; } Log.e(TAG, "一些事件发生在窗口外"); return false; } });
  • 你只需删除你的一段代码并添加我上面的代码,希望它能工作
  • 我尝试了你的代码并在它之后发布了我的问题。您的代码将 popupwindow 可聚焦状态设置为 false,这会在外部或内部触摸时关闭 popupwindow。但是,在我的场景中,有些项目我想被选中(切换),因此我需要关注。我该如何解决这个问题?
  • 这对我有用,可以在点击尺寸时隐藏 PW。这简直是​​奇迹!
【解决方案2】:

如果你想做一些动作,当它在窗口外被点击并且setFocusable() + setOutsideTouchable()你需要true时,你可以考虑使用setOnDismissListener。它的方法 onDismiss 在对话框对话框被关闭时按预期被调用:

  PopupWindow mPopupWindow = new PopupWindow(mRootView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  mPopupWindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
  mPopupWindow.setFocusable(true);
  mPopupWindow.setOutsideTouchable(true);
  mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        // some action ....
      }
  });

【讨论】:

  • 我试过你的解决方案,效果很好。但是,一旦执行此功能并且如果我单击任何按钮,应用程序就会崩溃,说明错误java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 试图解决但没有实现。有什么建议吗?
  • 当您尝试多次向视图层次结构添加视图时会发生这种情况。你应该寻找addView()getLayoutInflater().inflate(R.layout.xyz, parentView, true)
猜你喜欢
  • 2013-04-11
  • 2014-04-15
  • 1970-01-01
  • 2014-01-03
  • 2016-03-02
  • 1970-01-01
  • 2021-05-05
  • 2017-02-02
  • 2011-07-20
相关资源
最近更新 更多