很久没写博客,最近项目遇到需要获取输入法高度将EditText上移的效果,实现方式本来有几种
1.设置softInputMode WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
但是这样布局中如果有RecyclerView,或者ScrollView的时候,整个布局会向上滚动,但是我不想让他滚动,
执行编辑框上移
2.网上有一个SoftSoftInputKeyBoardUtil类,原理是通过获取可见高度来计算键盘高度,但是测试发现有些手机获取到的高度是不正确的,
有的高一些,有的低一些,(普遍是状态栏和导航栏的高度影响),特别是一些手机有导航栏一键隐藏功能,可以随时显示或隐藏导航栏,
设置隐藏时候,如果弹出输入法,又会自动加上导航栏,导致高度计算错误
下面介绍一种新的实现方式,从fitSystemWindow的灵感发现。(softInputMode需要是adjustResize)
设置fitSystemWindow后,会发现我们即使不用上面的监听方法,编辑框也能自己被输入法推上去,但是缺点是View会被添加一段白色的长条,这是由于适应适应系统窗口,系统会为我们给布局view加上pading,留出系统窗口占据的大小,但是有一些浸入式的实现方式可能会被破坏,需要我们手动修改状态栏颜色,从而有可能影响到其他页面的状态栏颜色(如果未及时重置颜色)
从源码中发现这样一个方法,在View和ViewCompat类中
View.java
/**
* Set an {@link OnApplyWindowInsetsListener} to take over the policy for applying
* window insets to this view. The listener's
* {@link OnApplyWindowInsetsListener#onApplyWindowInsets(View, WindowInsets) onApplyWindowInsets}
* method will be called instead of the view's
* {@link #onApplyWindowInsets(WindowInsets) onApplyWindowInsets} method.
*
* @param listener Listener to set
*
* @see #onApplyWindowInsets(WindowInsets)
*/
public void setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener listener) {
getListenerInfo().mOnApplyWindowInsetsListener = listener;
}
ViewCompat.java
/**
* Set an {@link OnApplyWindowInsetsListener} to take over the policy for applying
* window insets to this view. This will only take effect on devices with API 21 or above.
*/
public static void setOnApplyWindowInsetsListener(@NonNull View v,
final OnApplyWindowInsetsListener listener) {
if (Build.VERSION.SDK_INT >= 21) {
if (listener == null) {
v.setOnApplyWindowInsetsListener(null);
return;
}
v.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) {
WindowInsetsCompat compatInsets = WindowInsetsCompat.wrap(insets);
compatInsets = listener.onApplyWindowInsets(view, compatInsets);
return (WindowInsets) WindowInsetsCompat.unwrap(compatInsets);
}
});
}
}
两者没有太大差别,ViewCompat帮我们对View中的一些方法做了版本兼容
api介绍是这个listener会去接管view中对于窗口嵌入物的处理,
嵌入物的数据通过WindowInsets表示
稍微看下WindowInsets
重点看下mSystemWindowInsets
public final class WindowInsets {
/**
* Returns the left system window inset in pixels.
*
* The system window inset represents the area of a full-screen window that is
* partially or fully obscured by the status bar, navigation bar, IME or other system windows.
*
*
* @return The left system window inset
*
* 这个表示全屏模式下,部分或者全部被状态栏,导航栏,输入法或其他系统窗口遮挡的那部分区域
* 这些代码是我从这个类下两个地方拼接的,方便说明这个变量
*/
private Rect mSystemWindowInsets;
private Rect mWindowDecorInsets;
private Rect mStableInsets;
private Rect mTempRect;
private boolean mIsRound;
private DisplayCutout mDisplayCutout;
}
systemWindowInset就是我们要用的东西
/**
* Returns the left system window inset in pixels.
* @return The left system window inset
*/
public int getSystemWindowInsetLeft() {
return mSystemWindowInsets.left;
}
/**
* Returns the top system window inset in pixels.
* @return The top system window inset
*/
public int getSystemWindowInsetTop() {
return mSystemWindowInsets.top;
}
/**
* Returns the right system window inset in pixels.
* @return The right system window inset
*/
public int getSystemWindowInsetRight() {
return mSystemWindowInsets.right;
}
/**
* Returns the bottom system window inset in pixels.
* @return The bottom system window inset
*/
public int getSystemWindowInsetBottom() {
return mSystemWindowInsets.bottom;
}
这几个方法,分别获取左,上,右,下位置的遮挡
结论:我们可以给View设置这个Listener,获取WindowInsets,获取 getSystemWindowInsetBottom 得到底部被系统窗口嵌入物遮挡的距离,这样就由系统去计算移动的距离(不管你是导航栏还是输入法还是其他的,或者一起出现,遮挡窗口),然后将需要上移的view移动这个距离,通过设置padding或者marin即可。
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
setPadding(0, 0, 0, insets.systemWindowInsetBottom)
insets
}
这样就可以实现输入法弹起,编辑框上移,关闭输入法,编辑框复原位置
ViewCompat.setOnApplyWindowInsetsListener() 兼容到api 21 也就是5.0
View.setOnApplyWindowInsetsListener() 要求最低版本 20
更低版本可以通过覆写View onApplyWindowInsets来获取WindowInsets
再次提醒: windowSoftInputMode 设置未adjustResize
如果有错误,欢迎讨论,如果能解决你的问题,那就太高兴了
欢迎留言,让我知道这篇文章是否对大家有帮助