美文网首页程序人生
android全屏或沉浸式后,底部输入框被软键盘或虚拟导航栏遮挡

android全屏或沉浸式后,底部输入框被软键盘或虚拟导航栏遮挡

作者: 为自己代颜_ | 来源:发表于2022-05-20 00:03 被阅读0次

    原因:
    一般 *android:windowSoftInputMode="adjustResize" *就能解决软键盘遮住输入框的问题,但是当Activity设为Full Screen这个设置就无效了。
    直接上代码:

    class AndroidBugWorkaround private constructor(activity: Activity) {
        private val mChildOfContent: View
        private var usableHeightPrevious = 0
        private val frameLayoutParams: FrameLayout.LayoutParams
        private fun possiblyResizeChildOfContent() {
            val contentViewParams: ViewGroup.LayoutParams = frameLayoutParams
            //获取当前界面可视区域(注:可视区域指APP界面)
            //获取当前界面可视区域(注:可视区域指APP界面)
            val rect: Rect = computeUsableRect()
            //获取当前界面可视区域高度
            //获取当前界面可视区域高度
            val currentUsableHeight = rect.bottom - rect.top
            if (currentUsableHeight != usableHeightPrevious) {
                //获取根布局高度
                val rootViewHeight: Int = mChildOfContent.getRootView().getHeight()
                //计算出根布局高度与可视区域高度差值
                val heightDiff = rootViewHeight - currentUsableHeight
                //差值超过四分之一说明软键盘弹出
                if (heightDiff > rootViewHeight / 4) {
                    //全屏模式需要加上状态栏高度,否则低于软键盘高度的输入框弹起时与软键盘顶部会有偏差
                    contentViewParams.height =
                        rootViewHeight - heightDiff + getStatusHeight(mChildOfContent.getContext())
                } else {
                    //此处需要使用rect.bottom ,因为部分手机有虚拟导航且处于开启状态时会导致底部界面被虚拟导航遮挡
                    contentViewParams.height = rect.bottom
                }
                mChildOfContent.requestLayout()
                usableHeightPrevious = currentUsableHeight
            }
        }
    
        fun getStatusHeight(context: Context): Int {
            var result = 0
            val resourceId: Int =
                context.getResources().getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId)
            }
            return result
        }
    
    
        private fun computeUsableRect(): Rect {
            val r = Rect()
            mChildOfContent.getWindowVisibleDisplayFrame(r)
            return r
        }
    
    //    private fun computeUsableHeight(): Int {
    //        val r = Rect()
    //        mChildOfContent.getWindowVisibleDisplayFrame(r)
    //        return r
    //    }
    
        companion object {
            // For more information, see https://code.google.com/p/android/issues/detail?id=5497
            // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
            fun assistActivity(activity: Activity) {
                AndroidBugWorkaround(activity)
            }
        }
    
        init {
            val content = activity.findViewById<View>(R.id.content) as FrameLayout
            mChildOfContent = content.getChildAt(0)
            mChildOfContent.getViewTreeObserver()
                .addOnGlobalLayoutListener(OnGlobalLayoutListener { possiblyResizeChildOfContent() })
            frameLayoutParams = mChildOfContent?.layoutParams as FrameLayout.LayoutParams
        }
    }
    

    外部activity的onCreate方法直接调用

    AndroidBugWorkaround.assistActivity(this);
    

    相关文章

      网友评论

        本文标题:android全屏或沉浸式后,底部输入框被软键盘或虚拟导航栏遮挡

        本文链接:https://www.haomeiwen.com/subject/lzbxprtx.html