Jetpack Compose – 如何检查键盘是否打开或关闭

huangapple go评论89阅读模式
英文:

Jetpack Compose - how to check if keyboard is open or closed

问题

我正在使用Jetpack Compose,并尝试找到一种检测键盘是否打开的方法。

我尝试使用下面的代码,但是出现了一个错误,显示“未解析的引用:ime”。当我点击推荐的导入(下面显示的两个)时,这个错误仍然存在。

  1. import android.view.WindowInsets
  2. import android.view.WindowInsets.Type.ime
  3. @Composable
  4. fun signInView() {
  5. val isVisible = WindowInsets.ime.getBottom(LocalDensity.current) > 0
  6. }

我该如何解决这个问题?

英文:

I'm using Jetpack Compose and trying to find a way to detect if the keyboard is open.

I've tried to use the below code, but I get an error stating Unresolved reference: ime. When I click on the recommended imports (the 2 shown below), this error still remains.

  1. import android.view.WindowInsets
  2. import android.view.WindowInsets.Type.ime
  3. @Composable
  4. fun signInView() {
  5. val isVisible = WindowInsets.ime.getBottom(LocalDensity.current) > 0
  6. }

How can I resolve this?

答案1

得分: 1

  1. ** build.gradle 文件中为您的应用程序或模块添加所需的依赖项**
  2. ```gradle
  3. dependencies {
  4. implementation "androidx.compose.foundation:foundation:1.3.1"
  5. }
  6. android {
  7. buildFeatures {
  8. compose true
  9. }
  10. composeOptions {
  11. kotlinCompilerExtensionVersion = "1.3.2"
  12. }
  13. kotlinOptions {
  14. jvmTarget = "1.8"
  15. }
  16. }

示例:

  1. @Composable
  2. fun signInView() {
  3. var isVisible by remember { mutableStateOf(false) }
  4. val ime = androidx.compose.foundation.layout.WindowInsets.ime
  5. val navbar = androidx.compose.foundation.layout.WindowInsets.navigationBars
  6. var keyboardHeightDp by remember { mutableStateOf(0.dp) }
  7. val localDensity = LocalDensity.current
  8. LaunchedEffect(localDensity.density) {
  9. snapshotFlow {
  10. ime.getBottom(localDensity) - navbar.getBottom(localDensity)
  11. }.collect {
  12. val currentKeyboardHeightDp = (it / localDensity.density).dp
  13. keyboardHeightDp = maxOf(currentKeyboardHeightDp, keyboardHeightDp)
  14. isVisible = currentKeyboardHeightDp == keyboardHeightDp
  15. }
  16. }
  17. }
英文:

Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

  1. dependencies {
  2. implementation "androidx.compose.foundation:foundation:1.3.1"
  3. }
  4. android {
  5. buildFeatures {
  6. compose true
  7. }
  8. composeOptions {
  9. kotlinCompilerExtensionVersion = "1.3.2"
  10. }
  11. kotlinOptions {
  12. jvmTarget = "1.8"
  13. }
  14. }

Example:

  1. @Composable
  2. fun signInView() {
  3. var isVisible by remember { mutableStateOf(false) }
  4. val ime = androidx.compose.foundation.layout.WindowInsets.ime
  5. val navbar = androidx.compose.foundation.layout.WindowInsets.navigationBars
  6. var keyboardHeightDp by remember { mutableStateOf(0.dp) }
  7. val localDensity = LocalDensity.current
  8. LaunchedEffect(localDensity.density) {
  9. snapshotFlow {
  10. ime.getBottom(localDensity) - navbar.getBottom(localDensity)
  11. }.collect {
  12. val currentKeyboardHeightDp = (it / localDensity.density).dp
  13. keyboardHeightDp = maxOf(currentKeyboardHeightDp, keyboardHeightDp)
  14. isVisible = currentKeyboardHeightDp == keyboardHeightDp
  15. }
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年1月9日 02:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定