解决PopuWindow点击外部不销毁,点击back销毁(特此记录一下小白的成长路程)

第一次写博客,内容还不是100%原创,汗颜。。。。

在进入正文之前,先说说碰到的pop的坑,之前在模拟器4.4的系统下愉快的玩耍了许久,昨天测试了一下流程才发现pop在7.0系统上的问题。居然放大到了整个屏幕,我很无奈,等到今天找了一上午的资料(宽恕小白愚笨)。终于算是解决了问题(网上的方法),参考博客:https://blog.csdn.net/frained/article/details/53643427;值得一提的是设置的高度需要根据你的需求自己再做相应的修改吧。

回到正题,解决了7.0的适配问题之后发现pop点击外部不消失,嗯,我刚好这个需求;结果点击back键的时候,心理顿时一万只草泥马狂奔而过。整个activity销毁了。emmm,这不是我要的结果。动手设置了几个api。


具体的作用不就不细说了,网上有比我这解释的更详细的(我只是大概知道什么作用),点击back键可以销毁了,但是,点击外部也能销毁,我就呵呵了......

最后还是借鉴了github上的CustomPopWindow的设置外部点击不销毁的源码,git地址:https://github.com/pinguo-zhouwei/CustomPopwindow

关键代码:

 mPop.setFocusable(true);
        mPop.setBackgroundDrawable((Drawable) null);
        mPop.getContentView().setFocusable(true);
        mPop.getContentView().setFocusableInTouchMode(true);
        mPop.getContentView().setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == 4) {
                    mPop.dismiss();
                    return true;
                } else {
                    return false;
                }
            }
        });
        mPop.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                if (event.getAction() != 0 || x >= 0 && x < ViewGroup.LayoutParams.MATCH_PARENT && y >= 0 && y < ViewGroup.LayoutParams.MATCH_PARENT) {
                    if (event.getAction() == 4) {
//                        Log.e("CustomPopWindow", "out side ...");
                        return true;
                    } else {
                        return false;
                    }
                } else {
//                    Log.e("CustomPopWindow", "out side ");
//                    Log.e("CustomPopWindow", "width:" + CustomPopWindow.this.mPopupWindow.getWidth() + "height:" + CustomPopWindow.this.mPopupWindow.getHeight() + " x:" + x + " y  :" + y);
                    return true;
                }
            }
        });

对了,差点忘了说,之前查到一个说是解决7.0以上的popuwindow适配问题的终极解决方案,还没试,感兴趣的可以试一下:

https://blog.csdn.net/zhihui_520/article/details/79097161

开发之路还很漫长,共勉。如有侵权,联系我删除。

猜你喜欢

转载自blog.csdn.net/qq_38620843/article/details/79926047
今日推荐