重写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) {
popupwindow.dimiss();
finish()
return true;
}
return false;
}
});
popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
//下面可以做一些其他的popupwindow设置操作
转载自:https://blog.csdn.net/android_zhengyongbo/article/details/76013578