标签:
private PopupWindow mPopupWindow; //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单. private View mMenu; //popup window 里的view
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; }
/* * setBackgroundDrawable很重要,没有这个函数 back键和menu键关闭popwindow 很麻烦, * 有了它以后,就不用为popwindow内的控件分别设置setOnKeyListener和setOnTouchListener了 * 可以同时支持back,menu键和点popwindow外面关闭popwindow, */ mPopupWindow.setBackgroundDrawable(new ColorDrawable(-00000)); mPopupWindow.setFocusable(true); mPopupWindow.setOutsideTouchable(true);
PopupWindow(3)back,home 键无法关闭popupwindow的解决方案
标签:
原文地址:http://www.cnblogs.com/cocl/p/4520602.html