private PopupWindow mPopupWindow; //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单. private View mMenu; //popup window 里的view
方案1:
- 设置
mMenu.setFocusableInTouchMode(true);
mMenu.setOnKeyListener(this);
mMenu.setOnTouchListener(this);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
- 处理
@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:
/* * setBackgroundDrawable很重要,没有这个函数 back键和menu键关闭popwindow 很麻烦, * 有了它以后,就不用为popwindow内的控件分别设置setOnKeyListener和setOnTouchListener了 * 可以同时支持back,menu键和点popwindow外面关闭popwindow, */ mPopupWindow.setBackgroundDrawable(new ColorDrawable(-00000)); mPopupWindow.setFocusable(true); mPopupWindow.setOutsideTouchable(true);
时间: 2024-10-16 11:18:44