PopupWndow弹出时默认是点击返回键与界面 外都不会消失 ,要想实现,点击返回键与界面 外消失需要 做如下设置
popupWindow.setFocusable(true);//获取焦点
popupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景
如果只需要返回键使PopupWndow消失。需要重写view的onKeyListener,这个view应该是PopupWindow的contentview。代码如下
View layout = inflater.inflate(R.layout.account_dialog, (ViewGroup) activity.findViewById(R.id.account_dialog)); layout.setFocusable(true); layout.setFocusableInTouchMode(true); popupWindow = new PopupWindow(layout); popupWindow.setFocusable(true); // 重写onKeyListener layout.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { accountDialog.dismiss(); accountDialog = null; return true; } return false; } });