PopupWindow重写返回键

来源:互联网 发布:gta5线上ae86改装数据 编辑:程序博客网 时间:2024/06/05 12:06

重写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);
原创粉丝点击