Android登录界面,防止键盘遮挡输入框方案

1.LoginActivity配置 

android:windowSoftInputMode="adjustResize|stateHidden"

2.自定义Linerlayout重写onLayout。判断键盘弹出,隐藏显示相关控件即可

   var keyboardShowed = false

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (changed) {
            val width = right - left
            val height = bottom - top
            if (height.toFloat() / width.toFloat() < 4f / 3f) { // 如果高宽比小于4:3说明此时键盘弹出
                post {
                    loginBgWallLayout.visibility = View.INVISIBLE
                    val params = loginLayoutTop.layoutParams as LayoutParams
                    params.weight = 1.5f
                    keyboardShowed = true
                    loginLayoutTop.requestLayout()
                }
            } else {
                if (keyboardShowed) {
                    post {
                        loginBgWallLayout.visibility = View.VISIBLE
                        val params = loginLayoutTop.layoutParams as LayoutParams
                        params.weight = 6f
                        loginLayoutTop.requestLayout()
                    }
                }
            }
        }
    }

 

你可能感兴趣的:(学习笔记,Android)