概述
android开发过程中继续自PopupWindow的弹窗组件,默认点击外部区域会自动关闭,有些情况下设置this.setOutsideTouchable(false);之后依然无法取消这种关闭行为。
这样会导致有些自定义事件无法正常触发。
解决方法
此时通过setTouchInterceptor事件拦截器,可以实现。思路是判断传入的事件是否在弹窗UI内,是:则正常派发事件,否:则取消事件。如此可实现弹窗点击外部时不关闭。代码如下:
this.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Rect r = new Rect();
getContentView().getDrawingRect(r);
if(motionEvent.getX()>r.left && motionEvent.getX()<r.right
&& motionEvent.getY()>r.top && motionEvent.getY()<r.bottom){
//in
getContentView().dispatchTouchEvent(motionEvent);
}else{
//not in
}
return true;
}
});