美文网首页小技巧
Android 登录界面随软键盘弹出滑动

Android 登录界面随软键盘弹出滑动

作者: zarra1993 | 来源:发表于2017-02-23 15:32 被阅读0次

思路

在项目中遇到软键盘弹出遮挡登录按钮和登录输入框,需要将登录按钮和和输入框上移;
解决思路:监听软件盘的弹出,计算弹出后软键盘顶部的高度,计算登录按钮底部的高度,计算登录按钮以上的的整个布局需要的偏移量,使用属性动画进行偏移;

登录按钮的底部在Y轴的位置

 //按钮底部在Y轴的坐标
 int btnY = 0;
 btn_login.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
             @Override
             public void onGlobalLayout() {
                 int[] location = new int[2];
                 //获取登录按钮左上定点的坐标
                 btn_login.getLocationOnScreen(location);
                 btnY = location[1] + btn_login.getHeight();
                 btn_login.getViewTreeObserver().removeOnGlobalLayoutListener(this);
             }
         });

监听软键盘的显示与隐藏

/***
     * 判断软件盘是否弹出
     * @param v
     * @param listener
     * 备注:在不用的时候记得移除OnGlobalLayoutListener
     * */
    public static ViewTreeObserver.OnGlobalLayoutListener doMonitorSoftKeyboard(final View v,final OnSoftKeyBoardListener listener) {
        final ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                // 获取屏幕的可见范围保存在矩形r中
                v.getWindowVisibleDisplayFrame(r);
                int screenHeight = v.getRootView().getHeight();
                //软件盘高度 = 屏幕真实高度 - 屏幕可见范围的高度
                int heightDifference = screenHeight-r.bottom;
                boolean isSoftVisible = heightDifference > (screenHeight / 3);
                if(listener != null) {
                    listener.hasShow(isSoftVisible);
                }
            }
        };
        v.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        return layoutListener;
    }

在软键盘显示时计算登录按钮以上的布局需要偏移的距离 delta

    if(isShow) {
            Rect r = new Rect();
            ll_root.getWindowVisibleDisplayFrame(r);
            delta =  (float) Math.abs(r.bottom - btnY);
            AnimUtil.up(ll_login,-delta);
            AnimUtil.up(iv_logo,-delta/3);
        } else {
            AnimUtil.up(ll_login,0);
            AnimUtil.up(iv_logo,0);
        }

示例已经上传到github。还有点小问题,没找到原因。。记录一下

相关文章

网友评论

    本文标题:Android 登录界面随软键盘弹出滑动

    本文链接:https://www.haomeiwen.com/subject/tedbwttx.html