重写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);
本文介绍了一种方法,通过重写OnKeyDow方法并结合特定的代码配置,使得在Android应用中点击物理返回键时,PopupWindow不会消失。此技巧对于保持PopupWindow状态或实现特殊交互需求非常有用。
3万+

被折叠的 条评论
为什么被折叠?



