android开发软键盘遮挡登陆按钮
2015-09-02 12:30
531 查看
转自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,新的代码已经更新
在登陆和注册的时候经常会遇到键盘遮挡输入框和登陆按钮的情况,这时候我们需要做一些特殊处理将页面向上移动类似于微信这样
代码如下
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,新的代码已经更新
相关文章推荐
- Android (简介) TextWatcher监控EditText 升级方案
- android视频缓冲进度条SeekBar有断断续续的间断效果
- Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别
- android sharePreference的使用
- Android四大组建之Service
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- android性能优化--overdraw优化
- Android 查看內存使用
- Android如何防止apk程序被反编译
- 如何给你的Android 安装文件(APK)瘦身
- 使用 Android NDK 重用现有的 C 代码
- Android 动态布局实现多主题切换
- 使用 Android NDK 重用现有的 C 代码
- Android x86模拟器Intel Atom x86 System Image配置与使用方法
- Android小知识点、小疑问之 ListView中的灵异事件ListView跟Button 不能同时点击
- [轉]Android的内存泄漏和调试
- Android之Activity的启动方式
- Android 官方推荐 : DialogFragment 创建对话框
- android studio 获取SHA1值
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()