简体   繁体   English

如何在 Jetpack 组合 TextField/BasicTextField 中隐藏 cursor?

[英]How to hide cursor in Jetpack compose TextField/BasicTextField?

The solution I have so far is to use a Transparent Color for the cursor.到目前为止,我的解决方案是为 cursor 使用Transparent颜色。
I am looking for a better way to hide it if there is any.如果有的话,我正在寻找一种更好的方法来隐藏它。

cursorBrush = SolidColor(Transparent)

TextField should be focused, the keyboard should be open and the user should be able to type input. TextField 应该是焦点,键盘应该是打开的,并且用户应该能够键入输入。

截屏

The problem with this is I can still see the TextFieldCursorHandle after entering text.问题是我在输入文本后仍然可以看到TextFieldCursorHandle

在此处输入图像描述

  • In the BasicTextField you can hide the cursor using cursorBrush = SolidColor(Unspecified) .BasicTextField ,您可以使用cursorBrush = SolidColor(Unspecified)隐藏 cursor 。
  • In the TextField you can use the attribute colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)TextField中,您可以使用属性colors = TextFieldDefaults.textFieldColors(cursorColor = Color.Unspecified)

The TextFieldCursorHandle and the selected text use the color provided by LocalTextSelectionColors.current You override this color defining a custom TextSelectionColors : 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)
   )

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM