final PopupWindow mPopupWindow = new PopupWindow();
//设置popupWindow,当点击popupWindow外面的时候可以消失
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOutsideTouchable(true);
//设置监听
mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE && !mPopupWindow.isFocusable()) {
//如果焦点不在popupWindow上,且点击了外面,不再往下dispatch事件:
//不做任何响应,不 dismiss popupWindow
return true;
}
//否则default,往下dispatch事件:关掉popupWindow,
return false;
}
});