Android之如何解决popupWindow(pw.setFocusable(true))按返回键和menu键退出

来源:互联网 发布:淘宝客服晚上几点下班 编辑:程序博客网 时间:2024/05/16 09:38

先爆照:


问题:

使用过popupWindow的时候,我们为了让其它地方不可点击,我们一般会pw.setFocusable(true),但是当我们这样设置之后,问题出现了,按返回键或者menu菜单键没有反应,这是原因呢?

PopupWindow 跟我们的 Activity 不一样,因为我们在构造 PW 的时候往往不是继承来的,而是 new 出来的,所以不能使用重写 PW 的 onKeyDown() 之类的方法来截获键盘事件。

比如我们这样写,然并卵,毛反应都没有

    // 按一下退出程序    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            // 按返回退出popupwindow.dismiss();            if (mPopupWindow != null && mPopupWindow.isShowing()) {                mPopupWindow.dismiss();            }            return super.dispatchKeyEvent(event);        } else {            return super.dispatchKeyEvent(event);        }    }

解决办法:

1、

new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
关键在于最后一个参数,ADK 给出的提示是 Focusable,顾名思义就是该 PW 的 Focusable 属性,让它能够接受焦点。
当然你也可以手动设置它:
PW.setFocusable(true);
PW.setFocusableInTouchMode(true);  //为了保险起见加上这句
此时实际上还是不能起作用的,百度给出的结论是,必须加入下面这行作用未知的语句才能发挥作用:
 
pw.setBackgroundDrawable(new BitmapDrawable()); // www.linuxidc.com响应返回键必须的语句
请放心,设置 BackgroundDrawable 并不会改变你在配置文件中设置的背景颜色或图像。

2、

LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);View v = mLayoutInflater.inflate(R.layout.gridview, null);mPopupWindow = new PopupWindow(v, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);mPopupWindow.setOutsideTouchable(true);mPopupWindow.setFocusable(true);mPopupWindow.setBackgroundDrawable(new BitmapDrawable());mPopupWindow.showAtLocation(findViewById(R.id.line),Gravity.BOTTOM, 0, 0);GridView gv = (GridView) v.findViewById(R.id.gridView1);gv.setAdapter(new BodyAdapter());gv.setOnItemClickListener(this);v.setFocusableInTouchMode(true);v.setOnKeyListener(new android.view.View.OnKeyListener() {public boolean onKey(View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubif ((keyCode == KeyEvent.KEYCODE_MENU)&& (mPopupWindow.isShowing())&&i==1) {mPopupWindow.dismiss();return false;}i=1;return false;}});return false;我是这样解决的,当我们弹出来popwindow后,就相当于打开了另外一个“Activity“,所以不能再监听原来的,要到另外一个”Activity“也就是popwindow里面做监听,popwindow里面的最外层的view 我这里是linearlayout可以监听menu事件,在它里面设置就行

为了双保险,我2个都采取了,项目里面代码如下
 /**     * 点击评分,如果评分后,显示的弹出框     */    private void makePopupWindows() {        View view = LayoutInflater.from(HomeActivity.this).inflate(                R.layout.background, null);        mPopupWindow = new PopupWindow(view, mScreenWidth, 600);        WindowManager.LayoutParams params = getWindow().getAttributes();        params.alpha = 0.5f;        getWindow().setAttributes(params);        mPopupWindow.setOutsideTouchable(true);        mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点        mPopupWindow.setTouchable(true); // 设置PopupWindow可触摸        mPopupWindow.showAsDropDown(background_button);        view.setFocusableInTouchMode(true);        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());        view.setOnKeyListener(new android.view.View.OnKeyListener() {            public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub                if (keyCode == KeyEvent.KEYCODE_BACK &&mPopupWindow.isShowing()) {                    mPopupWindow.dismiss();                    WindowManager.LayoutParams params= getWindow().getAttributes();                    params.alpha=1.0f;                    getWindow().setAttributes(params);                    return true;                }                return false;            }        });    } 

好了 ,解决了,如果你也遇到这样的情况希望能帮到你。







0 0