思路:获取窗体可见的高度和窗体不可见的高度,然后将布局滚动到可见的区域,
可以获取软键盘的高度,然后将根布局滚动相应的距离
1.当界面出现变动都会调用这个监听事件: view.getViewTreeObserver().addOnGlobalLayoutListener
/**
* 解决在页面底部置输入框,输入法弹出遮挡部分输入框的问题
*
* @param root 页面根元素
* @param editLayout 被软键盘遮挡的输入框editText
* @param hasFocus 当前editText是否获取焦点
*/
public static void keyboardLayout(final View root,
final View editLayout, boolean hasFocus) {
// TODO Auto-generated method stub
root.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
// TODO Auto-generated method stub
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInVisibleHeigh = root.getRootView().getHeight() - rect.bottom;
//若不可视区域高度大于100,则键盘显示
if (hasFocus && rootInVisibleHeigh > 100) {
// Log.v("hjb", "不可视区域高度大于100,则键盘显示");
int[] location = new int[2];
//获取editLayout在窗体的坐标
editLayout.getLocationInWindow(location);
//计算root滚动高度,使editLayout在可见区域
int srollHeight = (location[1] + editLayout.getHeight()) - rect.bottom;
root.scrollTo(0, srollHeight);
} else {
//键盘隐藏
// Log.v("hjb", "不可视区域高度小于100,则键盘隐藏");
root.scrollTo(0, 0);
}
});
}
2.在被遮挡的EditText增加焦点的监听
mEtRoleName.setOnFocusChangeListener((v, hasFocus) -> {
//输入框被软件盘挡住
LinearLayout ll_create_meeting = view.findViewById(R.id.ll_create_meeting);
keyboardLayout(ll_create_meeting, mEtRoleName, hasFocus);
});
参考:1.https://blog.csdn.net/BinZuo/article/details/49446467
2.https://blog.csdn.net/zhangying1994/article/details/104126008