輸入框被遮擋
A: 開發(fā)童鞋胁澳,webview 評論框被輸入法遮擋看不見了,這個是不是该互?
B: 這個需要看一看C渍摺(內(nèi)心抓狂韭畸,啊啊啊)
android 開發(fā)過程中蔓搞,避免不了與webview 打交道胰丁,相信不少小伙伴,也遇到過喂分,webview 輸入框被輸入法遮擋問題锦庸。今天我們就來聊一聊這個老生常談的問題,輸入框被遮擋蒲祈,我們該如何解決甘萧。首先看看現(xiàn)狀圖:
遇到問題,話不多說梆掸,開始排查問題扬卷。 android Webview 輸入法被遮擋。處理方案存在如下兩種情況酸钦。一是activity 未設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN怪得,二是activity設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。
- activity未設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN卑硫,此種情況下可以通過在清單文件設置窗口軟鍵盤交互模式為
android:windowSoftInputMode="adjustResize"
即可解決徒恋。如下所示:
<activity
android:name=".web.WebActivity"
android:excludeFromRecents="false"
android:windowSoftInputMode="adjustResize" />
- activity 設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN時。解決方案為通過監(jiān)聽軟鍵盤的彈出欢伏,計算軟鍵盤的高度入挣,動態(tài)調(diào)整webView 的margin 來達到目的。
軟鍵盤高度計算
????在進行計算軟鍵盤高度之前硝拧,我們首先了解一下幾個基本概念财岔。如圖,由外向內(nèi)依次來看黑色方框河爹。最外層黑色框框框起來的范圍為decorView的視圖范圍匠璧,decorView是android 頂層的view,一般Activity 中setContentView設置的即是設置添加到decorView咸这。緊接著夷恍,第二個黑框為當前activity 的可見區(qū)域(不包含statusBar 以及navigationBar)。最后,一個黑框為decorView 的可見區(qū)域(不包含statusBar 以及navigationBar)酿雪。
????了解基本概念之后遏暴,我們可以簡單推導出鍵盤高度的計算公式。如下所示:
- 存在導航鍵情況
鍵盤高度 = 當前activity 高度 - decorView 可見區(qū)域的高度
- 不存在導航鍵情況指黎,即頁面全面屏的情況
鍵盤高度 = decorView 高度 - decorView 可見區(qū)域的底部
接下來我們看看界面布局朋凉,以及邏輯代碼的具體實現(xiàn)。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl_tool_bar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:src="@mipmap/ic_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/black"
android:textSize="20dp"
tools:text="網(wǎng)頁" />
</RelativeLayout>
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rl_tool_bar"
app:layout_constraintVertical_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
private lateinit var binding: ActivityWebBinding
private lateinit var layoutParams: ConstraintLayout.LayoutParams
private var currentHeight = 0
// 處理webView 軟鍵盤顯示
private fun handleSoftInput() {
layoutParams = binding.web.layoutParams as ConstraintLayout.LayoutParams
val decorView = window.decorView
decorView.viewTreeObserver.addOnGlobalLayoutListener {
val rect = Rect()
decorView.getWindowVisibleDisplayFrame(rect)
val keyboardMinHeight = dpToPx(100f)
val screenHeight =
if (hasNavigationBar(decorView)) resources.displayMetrics.heightPixels else decorView.height
val rectHeight = if(hasNavigationBar(decorView)) rect.height() else rect.bottom
val heightDiff = screenHeight - rectHeight
// 視圖樹變化高度大于100dp,認為鍵盤彈出
// currentHeight 防止界面頻繁刷新醋安,降低幀率杂彭,耗電
if (currentHeight != heightDiff && heightDiff > keyboardMinHeight) {
// 鍵盤彈出
currentHeight = heightDiff
layoutParams.bottomMargin = currentHeight
binding.web.requestLayout()
} else if (currentHeight != heightDiff && heightDiff < keyboardMinHeight) {
// 鍵盤收起
currentHeight = 0
layoutParams.bottomMargin = currentHeight
binding.web.requestLayout()
}
}
}
private fun dpToPx(value: Float): Int {
return (0.5f + value * resources.displayMetrics.density).toInt()
}
// 是否顯示導航欄
private fun hasNavigationBar(view: View): Boolean {
val compact = ViewCompat.getRootWindowInsets(view.findViewById(android.R.id.content))
compact?.apply {
return isVisible(WindowInsetsCompat.Type.navigationBars()) && getInsets(
WindowInsetsCompat.Type.navigationBars()
).bottom > 0
}
return false
}
???? 通過動態(tài)設置webView在視圖中的boottomMargin我們實現(xiàn)了webView輸入框不被鍵盤遮擋的目的。如下圖吓揪,看看此次最終效果亲怠。
????大功告成。