gpt4 book ai didi

android-jetpack-compose - 如何检测jetpack compose中的键盘打开和关闭?

转载 作者:行者123 更新时间:2023-12-04 13:07:10 28 4
gpt4 key购买 nike

我在 compose 中发现的唯一方法是使用 accompanist-insets并删除窗口插入。这会导致我的应用程序布局出现其他问题。
Android方式好像是this我可以将它传递给我的撰写应用程序并采取相应的行动。
Jetpack 组合还有另一种方法吗?

最佳答案

如果你想要声明,我找到了这个解决方案:

enum class Keyboard {
Opened, Closed
}

@Composable
fun keyboardAsState(): State<Keyboard> {
val keyboardState = remember { mutableStateOf(Keyboard.Closed) }
val view = LocalView.current
DisposableEffect(view) {
val onGlobalListener = ViewTreeObserver.OnGlobalLayoutListener {
val rect = Rect()
view.getWindowVisibleDisplayFrame(rect)
val screenHeight = view.rootView.height
val keypadHeight = screenHeight - rect.bottom
keyboardState.value = if (keypadHeight > screenHeight * 0.15) {
Keyboard.Opened
} else {
Keyboard.Closed
}
}
view.viewTreeObserver.addOnGlobalLayoutListener(onGlobalListener)

onDispose {
view.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalListener)
}
}

return keyboardState
}
并检测/检查值,你只需要这个:
val isKeyboardOpen by keyboardAsState() // Keyboard.Opened or Keyboard.Closed 

关于android-jetpack-compose - 如何检测jetpack compose中的键盘打开和关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68847559/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com