android开发软键盘遮挡登陆按钮

转自http://blog.csdn.net/yeaicff/article/details/44236853


在登陆和注册的时候经常会遇到键盘遮挡输入框和登陆按钮的情况,这时候我们需要做一些特殊处理将页面向上移动类似于微信这样


代码如下

private class OnKeyboardListener implements OnGlobalLayoutListener {
    	
    	private View scrollToView;

        private int lastYPos = 0;
    	
    	public OnKeyboardListener(View view) {
    		scrollToView = view;
    	}

        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            // 获取root在窗体的可视区域
            mContentLL.getWindowVisibleDisplayFrame(rect);
            // 获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
            int rootInvisibleHeight = mContentLL.getRootView()
                    .getHeight() - rect.bottom;
            // 若不可视区域高度大于100,则键盘显示
            if (rootInvisibleHeight > 100) {
                int[] location = new int[2];
                // 获取scrollToView在窗体的坐标
                scrollToView.getLocationInWindow(location);
                // 计算root滚动高度,使scrollToView在可见区域
                int scrollHeight = (location[1]
                        + scrollToView.getHeight() + DisplayUtil
                        .dip2px(mContext, 2)) - rect.bottom;
                scrollHeight += lastYPos;
                mContentLL.scrollTo(0, scrollHeight);
                lastYPos = scrollHeight;
            } else {
                // 键盘隐藏
                mContentLL.scrollTo(0, 0);
                lastYPos = 0;
            }
        
	}
    	
}

经验证,以前的代码会有一些BUG,新的代码已经更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值