设置popwindow
View popview = View.inflate(this, R.layout.popwindowmobanxuanzeshezhikuang, null);
// mpopupWindow = new PopupWindow(popview, (int) (DataCache.getInstance().phonewidth * 0.29), (int) (DataCache.getInstance().phoneheight * 0.35));
mpopupWindow2 = new PopupWindow(popview, 1836, 900);
//设置其获取焦点
mpopupWindow2.setFocusable(false);
//6.0前必须设置其背景图片,哪怕一个空的图
// mpopupWindow2.setTouchable(false);
mpopupWindow2.setBackgroundDrawable(new BitmapDrawable());
mpopupWindow2.setOutsideTouchable(false);
pop_mubanliebiaotitle= (TextView) popview.findViewById(R.id.pop_mubanliebiaotitle);
pop_quxiao= (Button) popview.findViewById(R.id.pop_mubanliebiaoshezhiquxiao);
pop_queding= (Button) popview.findViewById(R.id.pop_mubanliebiaoshezhiqueding);
mpopupWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//虚拟按键防止被遮盖
mpopupWindow2.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
CommonHelper.backgroundAlpha(YizhuluruActivity.this, 1f);//0.0-1.0
}
});
6.0后android 6.0 以后设置背景和没有设置背景都一样,view都会放在PopupWindow的一个内部ViewGroup(FramLayout)里;而 6.0以前的只有设置背景了才会放到一个ViewGroup里。这个ViewGroup把dispatchKeyEvent消耗掉了!
在Activity中拦截!! 原理不太清楚 待探讨
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {//6.0
if(mpopupWindow2!=null&&mpopupWindow2.isShowing()){
mpopupWindow2.dismiss();
return false;
}
if(mpopupWindow2!=null&&!mpopupWindow2.isShowing()){
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(mpopupWindow2!=null&&mpopupWindow2.isShowing()){
return false;
}
return super.dispatchTouchEvent(ev);
}