如何在Jetpack Compose中为TextField/BasicTextField隐藏TextFieldCursorHandle?

3
到目前为止,我的解决方案是使用“透明”颜色来隐藏光标。
如果有更好的方法来隐藏它,我正在寻找。
cursorBrush = SolidColor(Transparent)

应该使TextField获得焦点,打开键盘并允许用户输入。

截图

问题在于,即使输入文本后,我仍然可以看到TextFieldCursorHandle

输入图像说明在此处


是的,您可以将 cursorBrush = SolidColor(Color.Transparent) 应用于您的 BasicTextField - Gabriele Mariotti
@GabrieleMariotti,它无法隐藏“TextFieldCursorHandle”。 - Abhimanyu
1个回答

8
  • BasicTextField中,您可以使用cursorBrush = SolidColor(Unspecified)来隐藏光标。
  • TextField中,您可以使用属性colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)

TextFieldCursorHandle和选定的文本使用LocalTextSelectionColors.current提供的颜色。您可以通过定义自定义的TextSelectionColors来覆盖此颜色:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Color.Transparent,
    backgroundColor = Color.Transparent
)

CompositionLocalProvider(LocalTextSelectionColors provides customTextSelectionColors) {
   BasicTextField(
       value = text,
       onValueChange = {text = it},
       cursorBrush = SolidColor(Unspecified)
   )

   TextField(
       value = text,
       onValueChange = {text = it},
       colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)
   )

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接