android PopupWindow 监听返回键,

来源:互联网 发布:csgofps优化 编辑:程序博客网 时间:2024/06/05 18:11

 

    LayoutInflater li = (LayoutInflater) getBaseContext().getSystemService(
    LAYOUT_INFLATER_SERVICE);
   root = (View) li.inflate(R.layout.image, null);
   // 创建PopupWindow对象
  popupWindow = new PopupWindow(root, 280, 360);

popupWindow.setBackgroundDrawable(new BitmapDrawable());
     popupWindow.setFocusable(true);
     popupWindow.setFocusable(true);

   root.setOnKeyListener(new OnKeyListener()
   {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
     // TODO Auto-generated method stub
     if (keyCode == KeyEvent.KEYCODE_BACK)
     {
      Log.v("keyCode", "/" + keyCode);
       if(popupWindow != null) {
        popupWindow.dismiss();
        popupWindow = null;
      }
     }
      return false;
    }
 

PopupWindow  在执行之后不执此方法按手机的返回键  就会报错   也可以退出 PopupWindow
原创粉丝点击