PopupWindow(3)back,home 键无法关闭popupwindow的解决方案

private PopupWindow mPopupWindow;  //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单.
private View mMenu; //popup window 里的view

1方案1:

1.1 设置

mMenu.setFocusableInTouchMode(true);
mMenu.setOnKeyListener(this);
mMenu.setOnTouchListener(this);

mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);

1.2 处理

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_MENU:
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
break;
}
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mPopupWindow.dismiss();
return false;
}

2.方案2:不支持menu键

     /*
* setBackgroundDrawable很重要,没有这个函数 back键和menu键关闭popwindow 很麻烦,
* 有了它以后,就不用为popwindow内的控件分别设置setOnKeyListener和setOnTouchListener了
* 可以同时支持back和点popwindow外面关闭popwindow,
*/
mPopupWindow.setBackgroundDrawable(new ColorDrawable(-00000));//不支持menu键
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
上一篇:SynchronousQueue------TransferQueue源码分析


下一篇:Dapper inner join