全屏情况下Fragment中的webview中的文本框被软键盘挡住的问题

我是在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()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值