重写OnKeyDow配合下面代码,点击物理返回键PopWindow不消失
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup, null);
contentview.setFocusable(true); // 这个很重要
contentview.setFocusableInTouchMode(true);
final PopupWindow popupWindow = new PopupWindow(contentview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(false);// 这个很重要
popupWindow.setOutsideTouchable(false);
contentview.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);