android 解决输入法键盘遮盖布局问题
/**
* @param root 最外层布局,需要调整的布局
* @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
*/
private void controlKeyboardLayout(final View root, final View scrollToView) {
root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
//若不可视区域高度大于100,则键盘显示
if (rootInvisibleHeight > 100) {
int[] location = new int[2];
//获取scrollToView在窗体的坐标
scrollToView.getLocationInWindow(location);
//计算root滚动高度,使scrollToView在可见区域的底部
int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
root.scrollTo(0, srollHeight);
} else {
//键盘隐藏
root.scrollTo(0, 0);
}
}
});
}
效果图如下:
下面提供完整的代码及布局文件:
1. MainActivity
public class MainActivity extends Activity {
private LinearLayout mRoot;
private Button mSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRoot = (LinearLayout) findViewById(R.id.root);
mSubmit = (Button) findViewById(R.id.submit);
controlKeyboardLayout(mRoot, mSubmit);
}
/**
* @param root 最外层布局,需要调整的布局
* @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
*/
private void controlKeyboardLayout(final View root, final View scrollToView) {
root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
//若不可视区域高度大于100,则键盘显示
if (rootInvisibleHeight > 100) {
int[] location = new int[2];
//获取scrollToView在窗体的坐标
scrollToView.getLocationInWindow(location);
//计算root滚动高度,使scrollToView在可见区域
int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
root.scrollTo(0, srollHeight);
} else {
//键盘隐藏
root.scrollTo(0, 0);
}
}
});
}
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical" >
<EditText android:layout_width="fill_parent"
android:layout_height="50dip"
android:hint="edit1"/>
<EditText android:layout_width="fill_parent"
android:layout_height="50dip"
android:hint="edit2"/>
<EditText android:layout_width="fill_parent"
android:layout_height="50dip"
android:hint="edit3"/>
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="submit"/>
</LinearLayout>
android 解决输入法键盘遮盖布局问题的更多相关文章
- Android中如何解决输入法键盘和activity页面遮挡的问题
不希望遮挡设置activity属性android:windowSoftInputMode="adjustPan" 希望动态调整高度android:windowSoftInputMo ...
- Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果)
在个别时候,需要强制隐藏Android输入法键盘,如当前键盘正在显示,这个时候点击了侧滑面板,就要强制隐藏输入法键盘.网上常见的方法有: 1. InputMethodManager imm = (In ...
- android webview 输入法键盘遮挡输入框的问题
新建一个工具类: /** * 解决webView键盘遮挡问题的类 * Created by zqy on 2016/11/14. */ public class KeyBoardListener { ...
- Android解决软键盘弹出将布局顶到上面
有时候我们在下面的布局是一个RadioGroup,然后当页面中的EditText获得焦点的时候,会将地步的RadioGroup顶起来,这时候我们只需要在AndroidMainfest中RadioGro ...
- Android 关于显示键盘,布局错乱网上顶的问题
<activity android:name="com.taiyi.DiscussActivity" android:windowSoftInputMode="st ...
- 手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法
手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法 最近碰到Android微信内置浏览H5页面,因为其中的文本输入框(input)放置在靠近页面的中下方,点击文本框以后,则输入框会被弹出的手机 ...
- Android 输入法键盘和activity页面遮挡问题解决
本文主要介绍Android中如何解决输入法键盘和activity页面遮挡的问题. 总结: 不希望遮挡设置activity属性android:windowSoftInputMode="adju ...
- Android中软键盘弹出时关于布局的问题
当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncre ...
- Android中软键盘弹出时底部菜单上移问题
当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...
随机推荐
- u3d中的坐标系
任何子级游戏对象 (Child GameObject) 的检视器 (Inspector) 中的变换 (Transform) 值都会相对于父级 (Parent) 的变换 (Transform) 值而显示 ...
- 【教程】鼠标右键新建添加RTF文档
鼠标右键新建添加RTF文档 今天想将空间日志作个本地备份,但是苦于找不到适合的文本工具,因为一般的文本编辑工具都不支持贴图. 虽然word就可以满足我们的需求,但文件格式不用doc而是rtf 而 ...
- ajax实现返回数据是html类型的跨域问题
$.ajax({ url : _url, type : 'GET', dataType : 'html', xh ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- 修改AIX Noncomp内存占用比
解决AIX系统由于文件缓存在内存未释放引发的进程异常终止问题: topas: Noncomp占用过多内存 占用内存前十的进程: ps -ealf | head -1 ; ps -ealf | sort ...
- JAVA-JSP内置对象之application范围
相关资料:<21天学通Java Web开发> application范围1.就是指在整个服务器范围,直到服务器停止以后才会失效.2.通过使用application的setAttribute ...
- iOS微信支付demo运行报错解决如下
要接入微信支付的小伙伴,首先要下载一份官方demo(APP微信支付官方Demo下载),然后打开工程,准备大干一场. 1.编译报错 编译的时候居然直接报错了(orz) 错误提示: APP微信支付官方De ...
- 几种经典的Hash算法的实现(源代码)
来源声明: http://blog.minidx.com/2008/01/27/446.html 先保存下来,以备后面研究,现在还看不懂! 哈希算法将任意长度的二进制值映射为固定长度的较小二进制值,这 ...
- C语言 · 递归求二项式系数值
算法训练 6-1 递归求二项式系数值 时间限制:10.0s 内存限制:256.0MB 问题描述 样例输入 一个满足题目要求的输入范例.3 10 样例输出 与上面的样例输入对应的输出. ...
- <低风险投资之路>读书笔记
书在这里 低风险才能高收益 大的投资收益回撤,是长期高复合收益率的最大杀手 要赚钱先不赔钱 选择合理的投资组合与合理的仓位 盈利的关键不是你买了什么,而是你买了多少.这就是仓位管理的技巧,在确定性盈利 ...