我是在Fragment中使用webview的,不知道为什么在它的父级Activity里使用AndroidBug5497Workaround不起作用。在Fragment中添加如下代码起作用了。
class MyWebFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//解决全屏时软键盘挡住文本框的问题
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
mTargetView = view
mTargetViewViewTreeObserver = mTargetView?.viewTreeObserver
mTargetViewViewTreeObserver?.addOnGlobalLayoutListener(onGlobalLayoutListener)
}
override fun onDestroy() {
super.onDestroy()
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
if (mTargetViewViewTreeObserver?.isAlive == true) {
mTargetViewViewTreeObserver?.removeOnGlobalLayoutListener(onGlobalLayoutListener)
}
}
private var mTargetView: View? = null
private var mTargetViewViewTreeObserver: ViewTreeObserver? = null
private var onGlobalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (mTargetView != null) {
val visibleRect = Rect()
mTargetView?.getWindowVisibleDisplayFrame(visibleRect)
if (visibleRect.height() < mTargetView!!.height * 0.75f) {
val paddingLeft = visibleRect.left
val paddingTop = visibleRect.top
val paddingRight = mTargetView!!.width - visibleRect.right
val paddingBottom = mTargetView!!.height - visibleRect.bottom
mTargetView!!.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
} else {
enterImmersiveSticky(activity?.window)
mTargetView!!.setPadding(0, 0, 0, 0)
}
}
}
private fun enterImmersiveSticky(window: Window?) {
val decorView = window?.decorView
val uiOptions = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
decorView?.systemUiVisibility = uiOptions
}
}
提取出来一个工具类:
package com.Onestroke.drawing.util
import android.app.Activity
import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.view.Window
import android.view.WindowManager
class SoftKeyHideInputUtil(var activity: Activity, var view: View) {
private var mTargetView: View? = null
private var mTargetViewViewTreeObserver: ViewTreeObserver? = null
fun onCreate() {
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
mTargetView = view
mTargetViewViewTreeObserver = mTargetView?.viewTreeObserver
mTargetViewViewTreeObserver?.addOnGlobalLayoutListener(onGlobalLayoutListener)
}
private var onGlobalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (mTargetView != null) {
val visibleRect = Rect()
mTargetView?.getWindowVisibleDisplayFrame(visibleRect)
if (visibleRect.height() < mTargetView!!.height * 0.75f) {
val paddingLeft = visibleRect.left
val paddingTop = visibleRect.top
val paddingRight = mTargetView!!.width - visibleRect.right
val paddingBottom = mTargetView!!.height - visibleRect.bottom
mTargetView!!.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
} else {
enterImmersiveSticky(activity?.window)
mTargetView!!.setPadding(0, 0, 0, 0)
}
}
}
private fun enterImmersiveSticky(window: Window?) {
val decorView = window?.decorView
val uiOptions = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
decorView?.systemUiVisibility = uiOptions
}
fun onDestroy() {
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
if (mTargetViewViewTreeObserver?.isAlive == true) {
mTargetViewViewTreeObserver?.removeOnGlobalLayoutListener(onGlobalLayoutListener)
}
}
}
使用方法:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { mSoftKeyHideInputUtil = SoftKeyHideInputUtil(requireActivity(),view) mSoftKeyHideInputUtil?.onCreate() } override fun onDestroy() { super.onDestroy() mSoftKeyHideInputUtil?.onDestroy() }