【问题标题】:Jetpack compose LazyColumn or Scrollable Column and IME padding for TextField doesn't workJetpack 撰写 LazyColumn 或 Scrollable Column 以及 TextField 的 IME 填充不起作用
【发布时间】:2021-07-24 06:23:07
【问题描述】:

我正在尝试设置文本字段列表,当用户将焦点设置在底部的一个文本字段上时,我希望用户可以看到出现的 IME 软键盘和根据配置集填充的文本字段在我的清单文件 android:windowSoftInputMode="adjustPan" 中,但它第一次不起作用,它仅在列出的某些文本字段已经具有焦点时才起作用。

Behavior in video.

我在 onCreate 方法中的代码。

    // Turn off the decor fitting system windows, which allows us to handle insets,
    // including IME animations
    WindowCompat.setDecorFitsSystemWindows(window, false)

    setContent {

        // Provide WindowInsets to our content. We don't want to consume them, so that
        // they keep being pass down the view hierarchy (since we're using fragments).
        ProvideWindowInsets(consumeWindowInsets = false) {

            MyApplicationTheme {

                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background, modifier = Modifier.systemBarsPadding()) {

                    Column(modifier = Modifier.fillMaxHeight()) {

                        val list: List<@Composable () -> Unit> = (1..10).map {
                            {

                                Text(text = "$it")

                                Divider()

                                TextField(value = "", onValueChange = {}, modifier = Modifier.navigationBarsWithImePadding(),)
                            }
                        }

                        LazyColumn(modifier = Modifier.fillMaxSize().weight(1F)) {

                            itemsIndexed(list) { index, inputText ->

                                inputText()
                            }
                        }
                    }
                }
            }
        }
    }

【问题讨论】:

  • 您找到解决方案了吗?我也面临同样的问题。
  • 视频链接失效

标签: android-softkeyboard android-jetpack-compose ime lazylist


【解决方案1】:

如果您的问题仍然存在,请查看Insets for Jetpack Compose

只添加这个依赖:

implementation 'com.google.accompanist:accompanist-insets:{insetsVersion}'

并使用Modifier.imePadding() 或自定义解决方案:

@Composable
fun ImeAvoidingBox() {
    val insets = LocalWindowInsets.current

    val imeBottom = with(LocalDensity.current) { insets.ime.bottom.toDp() }
    Box(Modifier.padding(bottom = imeBottom))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2022-08-23
    • 2022-06-25
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多