• webView 键盘遮挡输入框处理


    package com.hconline.iso.uicore.utils
    
    import android.app.Activity
    import android.graphics.Rect
    import android.os.Build
    import android.provider.Settings
    import android.util.Log
    import android.view.View
    import android.view.WindowManager
    import android.widget.FrameLayout
    
    /**
     * webView 键盘遮挡输入框处理
    * 适用于 全屏/非全屏/有底部导航栏/无底部导航栏 */ class WebViewKeyBoardListener( private val activity: Activity ) { private var mChildOfContent: View? = null private var usableHeightPrevious: Int = 0 private var frameLayoutParams: FrameLayout.LayoutParams? = null fun init() { val content = activity .findViewById<View>(android.R.id.content) as FrameLayout mChildOfContent = content.getChildAt(0) mChildOfContent!!.viewTreeObserver.addOnGlobalLayoutListener { possiblyResizeChildOfContent() } frameLayoutParams = mChildOfContent!! .layoutParams as FrameLayout.LayoutParams } private fun possiblyResizeChildOfContent() { val usableHeightNow = computeUsableHeight() if (usableHeightNow != usableHeightPrevious) { val usableHeightSansKeyboard = mChildOfContent!!.rootView .height - getNavHeight() val heightDifference = usableHeightSansKeyboard - usableHeightNow if (heightDifference > usableHeightSansKeyboard / 4) { // keyboard probably just became visible frameLayoutParams!!.height = usableHeightSansKeyboard - heightDifference } else { // keyboard probably just became hidden frameLayoutParams!!.height = usableHeightSansKeyboard } mChildOfContent!!.requestLayout() usableHeightPrevious = usableHeightNow } } private fun computeUsableHeight(): Int { val r = Rect() mChildOfContent!!.getWindowVisibleDisplayFrame(r) return r.bottom - r.top } private fun getNavHeight(): Int { val resources = activity.resources val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { //判断底部导航栏是否为显示状态 val navigationBarShowing = isNavigationBarShowing() if (navigationBarShowing) { val height = resources.getDimensionPixelSize(resourceId) return height } } return 0 } private fun isNavigationBarShowing(): Boolean { //判断手机底部是否支持导航栏显示 val haveNavigationBar = checkDeviceHasNavigationBar() if (haveNavigationBar) { if (Build.VERSION.SDK_INT >= 17) { val brand = Build.BRAND val mDeviceInfo = when { brand.toUpperCase() == "HUAWEI" -> "navigationbar_is_min" brand.toUpperCase() == "XIAOMI" -> "force_fsg_nav_bar" brand.toUpperCase() == "VIVO" -> "navigation_gesture_on" brand.toUpperCase() == "OPPO" -> "navigation_gesture_on" else -> "navigationbar_is_min" } //是否全屏 val isFullScree = (activity.window.attributes.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN if (Settings.Global.getInt( activity.contentResolver, mDeviceInfo, 0 ) == 0 && !isFullScree ) { return true } } } return false } private fun checkDeviceHasNavigationBar(): Boolean { var hasNavigationBar = false val rs = activity.resources val id = rs.getIdentifier("config_showNavigationBar", "bool", "android") if (id > 0) { hasNavigationBar = rs.getBoolean(id) } try { val systemPropertiesClass = Class.forName("android.os.SystemProperties"); val m = systemPropertiesClass.getMethod("get", String::class.java) val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String if ("1" == navBarOverride) { hasNavigationBar = false } else if ("0" == navBarOverride) { hasNavigationBar = true } } catch (e: Exception) { e.printStackTrace() } return hasNavigationBar } companion object { fun getInstance(activity: Activity): WebViewKeyBoardListener { val keyBoardListener = WebViewKeyBoardListener(activity) return keyBoardListener } } }

      

    ggband
  • 相关阅读:
    vs code 编译python 输出到调试控制台
    vs code个性化设置
    IDEA 简拼输入
    微信小程序 audio组件 默认控件 无法隐藏/一直显示/改了controls=‘false’也没用2019/5/28
    win10的cortana搜索显示空白
    微信小程序tabbar不显示2019.04.06
    读《提问的智慧》有感
    CLion 控制台输出内容乱码问题的解决方法
    vs code C语言环境搭建
    利用python的爬虫技术爬去糗事百科的段子
  • 原文地址:https://www.cnblogs.com/ggband/p/12205028.html
Copyright © 2020-2023  润新知