【问题标题】:TextField IME padding in LazyColumn , ComposeLazyColumn 中的 TextField IME 填充,撰写
【发布时间】:2021-04-17 14:02:26
【问题描述】:

问题:TextField(惰性列内)文本位于键盘下方

说明:

我有一个包含显示文本字段的项目列表的 LazyColumn,在清单中,活动有 windowSoftInputMode="adjustResize",我还设置了标志 WindowCompat.setDecorFitsSystemWindows(window,false)在 setContent 之前的 onCreate 方法中,我想让文本始终出现在键盘上方以获得更流畅的编辑体验!

使用提供 Window Insets 的 Accompanist Library 像这样为 Box 提供填充

Box(modifier = Modifier.weight(1f).navigationBarsWithImePadding()){
    LazyColumn() {
         items(myItems) { item->
              ItemComposable(item)
         }
    }
}

如您所见,框上有 navigationBarsWithImePadding,但由于文本位于键盘下方,它不起作用,我尝试在 LazyColumn 上设置修饰符,但随后它提供了 LazyColumn 相对于其他项目之外的填充盒子!

所以我尝试了contentPadding

LazyColumn(contentPadding=insets.ime.toPaddingValues(additionalBottom=insets.navigationBars.bottom.dp)) {
    items(editor.blocks) { block ->
        RenderBlock(block)
    }
}

再次没有用,因为内容填充应用于最后一项/或之后,键盘在文本上方

将 LazyColumn 替换为简单的 Column 并使用 verticalScroll 修饰符会导致同样的问题,因为列表可以很长,因此需要垂直滚动

可能的解决方案

val bringIntoViewRequester = remember { BringIntoViewRequester() }

TextField(
  modifier = Modifier
        .fillMaxWidth()
        .bringIntoViewRequester(bringIntoViewRequester),
  value = name,
  onValueChange = {
     name = it
     scope.launch {
        relocationRequestor.bringIntoView()
     }
  }
)

【问题讨论】:

    标签: android android-jetpack-compose


    【解决方案1】:

    如果使用Column,可以参考以下代码sn -p:

    Column(
        modifier = Modifier
            // other modifiers
            .verticalScroll(scrollState, reverseScrolling = true)
            .navigationBarsWithImePadding(),
        verticalArrangement = Arrangement.Bottom,
    )
    

    【讨论】:

      【解决方案2】:

      最后我有一个solution。只需按照以下步骤操作:

      第 1 步:在您的 AndroidManifest.xml 中

       <activity
          ...
          android:windowSoftInputMode="adjustResize">
       </activity>
      

      第 2 步:在您的活动中

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      
          WindowCompat.setDecorFitsSystemWindows(window, false)
      
          setContent {
      
              AppTheme {
      
                 ProvideWindowInsets(
                     windowInsetsAnimationsEnabled = true,
                     consumeWindowInsets = false,
                 ) {
                    // your content
                   }
               }
           }
      

      第 3 步:

      LazyColumn(
        modifier = Modifier
          .navigationBarsWithImePadding()
          .verticalScroll(state = rememberScrollState())
          .height(LocalConfiguration.current.screenHeightDp.dp)
          .fillMaxWidth(),
       ) {
            // your TextField items
         }
      

      第 4 步:

       // init your CoroutineScope
       val  coroutineScope = rememberCoroutineScope()
      
       // init your BringIntoViewRequester
       val  bringIntoViewRequester = BringIntoViewRequester()
      
       // use them in your TextField modifier
       modifier = Modifier
         /* ... your other modifiers*/
          .bringIntoViewRequester(bringIntoViewRequester)
          .onFocusEvent {
              if (it.isFocused || it.hasFocus) {
                  coroutineScope.launch {
                      delay(250)
                      bringIntoViewRequester.bringIntoView()
                  }
              }
          }
      }
      

      希望对你有帮助。

      【讨论】:

      • 最好在这里总结一下解决方案。存在问题是链接之外的信息,因此这不仅仅是 100% 链接。但它非常接近。
      • 谢谢@Yunnosch,我总结了上面的解决方案
      • 我建议保留链接。因为 a) 问题链接仍然是有价值的信息 b) 它可以保护您,根据需要提供尽可能多的贡献。
      • 对我来说它不起作用。如果在调用 bringIntoView() 之前键盘覆盖了该字段,则该字段超出了合成并失去焦点。 250 毫秒的延迟在某些情况下可以工作,但最近的一些三星设备(在 Galaxy s20 FE 上测试)键盘上基本没有动画。
      • 我提出了一个解决方案另一个解决方案here。在我的情况下,我手动滚动了保持字段可见所需的确切滚动量的 LazyColumn。我还在 LazyColumn 的底部添加了一个高度 = totalKeyboardHeight - currentKeyboardHeight 的项目,以保持lazycolumn 始终可滚动。当一个字段被聚焦时,我立即滚动所需的确切数量的延迟列,因为键盘的动画必须比延迟列的滚动慢。
      猜你喜欢
      • 2021-07-24
      • 2022-08-23
      • 2021-07-12
      • 2021-08-07
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多