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);

 

相关文章:

  • 2022-02-27
  • 2021-09-25
  • 2021-11-07
  • 2021-09-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2021-06-08
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案