简体   繁体   English

WindowInsetsCompat.isVisible(WindowInsetsCompat.Type.ime()) 为 Android 23-29 返回 true,尽管键盘未打开

[英]WindowInsetsCompat.isVisible(WindowInsetsCompat.Type.ime()) returns true for Android 23-29, though keyboard is not opened

If keyboard is opened the next code returns true (for showingKeyboard ) on Android 11 (30 API) or false otherwise:如果打开键盘,则下一个代码在 Android 11(30 API)上返回true (用于显示showingKeyboard ),否则返回false

private fun registerGlobalLayoutListener() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        window.decorView.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener)
    }
}

private fun unregisterGlobalLayoutListener() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        window.decorView.viewTreeObserver.removeOnGlobalLayoutListener(globalLayoutListener)
    }
}

@RequiresApi(Build.VERSION_CODES.M)
private val globalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
    val showingKeyboard =
        WindowInsetsCompat.toWindowInsetsCompat(window.decorView.rootWindowInsets)
            .isVisible(WindowInsetsCompat.Type.ime())
    viewModel.setKeyboardShowing(showingKeyboard)
}

On Android 6-10 (23-29) it returns true even if keyboard is not opened (not visible)在 Android 6-10 (23-29) 上,即使键盘未打开(不可见),它也会返回true

Try adding android:windowSoftInputMode="adjustResize" to your activity in the AndroidManifest.xml file.尝试在 AndroidManifest.xml 文件android:windowSoftInputMode="adjustResize"到您的活动中。

Also check this answer for more details: https://stackoverflow.com/a/66805360/949280另请查看此答案以获取更多详细信息: https : //stackoverflow.com/a/66805360/949280

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM