webview键盘被遮挡问题

Posted by on November 19, 2020
在某些时候,我们Android并不能像iOS那样,在webview中点击键盘并且被抬起,需要我们去做处理
  • 第一种是使用 AndroidBug5497Workaround 这个方法,但是这方法在华为手机和又底部虚拟按键的手机底部会有变形

  • 第二种方式就是动态改变webveiw的 setMargins 距离bootom的距离监听,这个高度就是键盘的高度,因为目的就是不让键盘盖住输入框 所以高出这个高度就可以了。

   //处理键盘弹出遮挡输入框问题
        val lp = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 0)
        lp.topToBottom = iv_back.id
        lp.leftToLeft = vm_parent.id
        lp.rightToRight = vm_parent.id
        lp.bottomToBottom = vm_parent.id

        SoftKeyboardUtils().apply {
            var webBottomHeight: Int = 0
            getSoftKeyboardHeight(
                webView_content,
                object : SoftKeyboardUtils.OnGetSoftHeightListener {
                    override fun onShowed(height: Int) {
                        webBottomHeight = height
                        "height = $height".logI("SoftKeyboardUtils")
                    }
                })
            doMonitorSoftKeyWord(
                webView_content,
                object : SoftKeyboardUtils.OnSoftKeyWordShowListener {
                    override fun hasShow(isShow: Boolean) {
//                    "isShow = $isShow".logI("SoftKeyboardUtils")
                        if (onPageFinish)
                            isShow.thenNoResult {
                                lp.setMargins(0, 0, 0, webBottomHeight)
                                webView_content.layoutParams = lp
                            } elseNoResult {
                                lp.setMargins(0, 0, 0, 0)
                                webView_content.layoutParams = lp
                            }
                    }
                })
        }



import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener




class SoftKeyboardUtils {
    private var isFirst = true

    interface OnGetSoftHeightListener {
        fun onShowed(height: Int)
    }

    interface OnSoftKeyWordShowListener {
        fun hasShow(isShow: Boolean)
    }

    /** * 获取软键盘的高度 * *
     * @param rootView *
     * @param listener
     */
    fun getSoftKeyboardHeight(
        rootView: View,
        listener: OnGetSoftHeightListener?
    ) {
        val layoutListener: OnGlobalLayoutListener = object : OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                if (isFirst) {
                    val rect = Rect()
                    rootView.getWindowVisibleDisplayFrame(rect)
                    val screenHeight: Int = rootView.rootView.height
                    val heightDifference: Int = screenHeight - rect.bottom
                    //设置一个阀值来判断软键盘是否弹出
                    val visible = heightDifference > screenHeight / 3
                    if (visible) {
                        isFirst = false
                        listener?.onShowed(heightDifference)
                        rootView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                    }
                }
            }
        }
        rootView.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
    }


    /** * 判断软键盘是否弹出
     * * @param rootView
     * @param listener
     * 备注:在不用的时候记得移除OnGlobalLayoutListener
     */
    fun doMonitorSoftKeyWord(
        rootView: View,
        listener: OnSoftKeyWordShowListener?
    ): OnGlobalLayoutListener? {
        val layoutListener = OnGlobalLayoutListener {
            val rect = Rect()
            rootView.getWindowVisibleDisplayFrame(rect)
            val screenHeight: Int = rootView.rootView.height
//            (rect.bottom.toString() + "#" + screenHeight).logI()
            val heightDifference: Int = screenHeight - rect.bottom
            val visible = heightDifference > screenHeight / 3
            listener?.hasShow(visible)
        }
        rootView.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
        return layoutListener
    }
}
  • 当键盘收起的时候在恢复高度就可以了