PopupWindow TouchInterceptor不工作(PopupWindow TouchI

2019-07-22 16:35发布

我想测试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);
}

该弹出窗口显示正确,通过似乎触摸拦截器完全不工作的方式:我没有得到任何的日志信息,当然,如果按它的外面弹出是不是解雇。

有一些进一步的财产我都在弹出窗口或在活动的主机它来设置?

Answer 1:

 pw.setBackgroundDrawable (new BitmapDrawable());
 pw.setFocusable(false);
 pw.setOutsideTouchable(true); 

使用此代码,希望这是有益的



Answer 2:

如果你想要做一些动作,当它被点击窗口和两个外setFocusable() + setOutsideTouchable()你需要真正的,你可以考虑使用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 ....
      }
  });


文章来源: PopupWindow TouchInterceptor not working