软键盘遮挡住popuwindow里面的输入框

问题截图
bug.png

如上,我红色指着的方向是一个输入框,它被挡住了。我整个灰色的是popuwindow

参考的网站

Android中获取软键盘状态和软键盘高度

代码实现
  //屏幕高度
    int screenHeight = 0;
    public interface OnSoftKeyboardStateChangedListener {
        public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
    }
    //注册软键盘状态变化监听
    public void addSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
        if (listener != null) {
            mKeyboardStateListeners.add(listener);
        }
    }
    //取消软键盘状态变化监听
    public void removeSoftKeyboardChangedListener(SchedulingActivity.OnSoftKeyboardStateChangedListener listener) {
        if (listener != null) {
            mKeyboardStateListeners.remove(listener);
        }
    }
    private ArrayList<SchedulingActivity.OnSoftKeyboardStateChangedListener> mKeyboardStateListeners;      //软键盘状态监听列表
    private ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener;
    private boolean mIsSoftKeyboardShowing;
    protected void onCreate(Bundle savedInstanceState) {
       WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenHeight = dm.heightPixels; // 屏幕高度(像素)
        //屏幕高度
        mIsSoftKeyboardShowing = false;
        mKeyboardStateListeners = new ArrayList<SchedulingActivity.OnSoftKeyboardStateChangedListener>();
        mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //判断窗口可见区域大小
                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
                int heightDifference = screenHeight - (r.bottom - r.top);
                boolean isKeyboardShowing = heightDifference > screenHeight / 3;

                //如果之前软键盘状态为显示,现在为关闭,或者之前为关闭,现在为显示,则表示软键盘的状态发生了改变
                if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
                    mIsSoftKeyboardShowing = isKeyboardShowing;
                    for (int i = 0; i < mKeyboardStateListeners.size(); i++) {
                        SchedulingActivity.OnSoftKeyboardStateChangedListener listener = mKeyboardStateListeners.get(i);
                        listener.OnSoftKeyboardStateChanged(mIsSoftKeyboardShowing, heightDifference);
                    }
                }
            }
        };
        //注册布局变化监听
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
        showBottomView();
}

 public void showBottomView() {
   if (bottomView == null) {
           addSoftKeyboardChangedListener(new SchedulingActivity.OnSoftKeyboardStateChangedListener() {
                @Override
                public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight) {
                    if (isKeyBoardShow) {//软键盘显示,这里设置了控制底部多少距离,各位可以在这里对界面进行控制
                        int paddingBottom = DensityUtil.dip2px(BAFApplication.getApplication(), linearLayoutPopuMenuParent.getPaddingBottom() + 30);
                        linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), paddingBottom);
                    } else {//软键盘没有显示,当软键盘没有显示的时候,就要还原
                        linearLayoutPopuMenuParent.setPadding(linearLayoutPopuMenuParent.getPaddingLeft(), linearLayoutPopuMenuParent.getPaddingTop(), linearLayoutPopuMenuParent.getPaddingRight(), 0);
                    }
                }
            });
     }
}

protected void onDestroy() {
    //移除布局变化监听
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutChangeListener);
    } else {
        getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutChangeListener);
    }
    super.onDestroy();
};





工具类

public class DensityUtil {
    private static DensityUtil densityUtil;

    public static DensityUtil getInstance() {
        if (densityUtil == null) {
            densityUtil = new DensityUtil();
        }
        return densityUtil;//返回一个实例
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
fixbug.png
优化 2019-10-12

将工具方法抽离出来
使用教程

 KeyBordChangeUtils   keyBordChangeUtils = new KeyBordChangeUtils(this.getActivity());
 if (keyBordChangeUtils!=null){
          //注册监听
                    keyBordChangeUtils.addSoftKeyboardChangedListener(new KeyBordChangeUtils.OnSoftKeyboardStateChangedListener() {
                        @Override
                        public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight) {
                            if (productInfoPopuwindow==null){
                                return;
                            }
                            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) productInfoPopuwindow.getPopuwindowLinlayoutParent().getLayoutParams();
                            if (isKeyBoardShow) {//软键盘显示,这里设置了控制底部多少距离
                                layoutParams.bottomMargin = keyboardHeight +DensityUtil.getInstance().dip2px(10);
                                productInfoPopuwindow.getPopuwindowLinlayoutParent().setLayoutParams(layoutParams);
                            } else {//软键盘没有显示,当软键盘没有显示的时候,就要还原
                                layoutParams.bottomMargin = 0;
                                productInfoPopuwindow.getPopuwindowLinlayoutParent().setLayoutParams(layoutParams);
                            }
                        }
                    });
                }
注意点:

keyboardHeight:包含了状态栏的高度

/**
 * @date :2019/10/12 0012
 * @author : gaoxiaoxiong
 * @description:软键盘监听
 **/ 
public class KeyBordChangeUtils {
    private WeakReference<Activity> weakReference;
    
    int screenHeight = 0;
    public interface OnSoftKeyboardStateChangedListener {
        void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
    }
    //注册软键盘状态变化监听
    public void addSoftKeyboardChangedListener(OnSoftKeyboardStateChangedListener listener) {
        if (listener != null) {
            mKeyboardStateListeners.add(listener);
        }
    }
    //取消软键盘状态变化监听
    public void removeSoftKeyboardChangedListener(OnSoftKeyboardStateChangedListener listener) {
        if (listener != null) {
            mKeyboardStateListeners.remove(listener);
        }
    }
    private ArrayList<OnSoftKeyboardStateChangedListener> mKeyboardStateListeners;      //软键盘状态监听列表
    private ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener;
    private boolean mIsSoftKeyboardShowing;

    
    public KeyBordChangeUtils(Activity activity) {
        if (weakReference == null){
            weakReference = new WeakReference<Activity>(activity);
        }
        init();
    }
    
    private void init(){
        //监听软键盘
        WindowManager wm = (WindowManager) weakReference.get().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenHeight = dm.heightPixels; // 屏幕高度(像素)
        //屏幕高度
        mIsSoftKeyboardShowing = false;
        mKeyboardStateListeners = new ArrayList<OnSoftKeyboardStateChangedListener>();
        mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //判断窗口可见区域大小
                Rect r = new Rect();
                weakReference.get().getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
                int heightDifference = screenHeight - (r.bottom - r.top);
                boolean isKeyboardShowing = heightDifference > screenHeight / 3;

                //如果之前软键盘状态为显示,现在为关闭,或者之前为关闭,现在为显示,则表示软键盘的状态发生了改变
                if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
                    mIsSoftKeyboardShowing = isKeyboardShowing;
                    //这里是回调,通知软键盘已经发生了变化
                    for (int i = 0; i < mKeyboardStateListeners.size(); i++) {
                        OnSoftKeyboardStateChangedListener listener = mKeyboardStateListeners.get(i);
                        listener.OnSoftKeyboardStateChanged(mIsSoftKeyboardShowing, heightDifference);
                    }
                }
            }
        };
        //注册布局变化监听
        weakReference.get().getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
    }

    /**
     * @date :2019/10/12 0012
     * @author : gaoxiaoxiong
     * @description:销毁
     **/
    public void onDestory(){
        if (mKeyboardStateListeners!=null){
            mKeyboardStateListeners.clear();
        }
        if (weakReference!=null){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                weakReference.get().getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutChangeListener);
            } else {
                weakReference.get().getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutChangeListener);
            }
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,188评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,464评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,562评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,893评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,917评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,708评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,430评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,342评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,801评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,976评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,115评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,804评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,458评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,008评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,135评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,365评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,055评论 2 355

推荐阅读更多精彩内容