Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。

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

Android Jetpack Compose cannot change BasicTextField cursor thumb color

问题

目前我正在使用Android Jetpack Compose的BasicTextField。当我更改光标颜色时,我希望光标手柄的颜色也会相应更改为相同的颜色。但实际情况是光标手柄显示为不同的颜色。这是一个错误吗,还是我设置错了什么?

这是我所设置的内容:

colors = TextFieldDefaults.textFieldColors(
    backgroundColor = Color.Transparent,
    focusedIndicatorColor = colorResource(id = R.color.accent),
    unfocusedIndicatorColor = colorResource(id = R.color.lightest_grey),
    focusedLabelColor = colorResource(id = R.color.secondary_20),
    unfocusedLabelColor = colorResource(id = R.color.light_grey),
    textColor = colorResource(id = R.color.secondary),
    cursorColor = colorResource(id = R.color.secondary),
)

Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。

英文:

Currently I'm working with android jetpack compose BasicTextField. And when I change cursor color, I expect the cursor handle to be changed with the same color as well. But it turns out with a different color. Is this a bug or I did set something wrong?

Here is what I have set

colors = TextFieldDefaults.textFieldColors(
    backgroundColor = Color.Transparent,
    focusedIndicatorColor = colorResource(id = R.color.accent),
    unfocusedIndicatorColor = colorResource(id = R.color.lightest_grey),
    focusedLabelColor = colorResource(id = R.color.secondary_20),
    unfocusedLabelColor = colorResource(id = R.color.light_grey),
    textColor = colorResource(id = R.color.secondary),
    cursorColor = colorResource(id = R.color.secondary),
  )

Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。

答案1

得分: 11

你需要提供一个自定义的**TextSelectionColors**。

类似这样:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Green,
    backgroundColor = Red
)

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

Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。
Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。

英文:

You have to provide a custom TextSelectionColors.

Something like:

val customTextSelectionColors = TextSelectionColors(
    handleColor = Green,
    backgroundColor = Red
)

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

Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。
Android Jetpack Compose 无法更改 BasicTextField 光标指示器颜色。

huangapple
  • 本文由 发表于 2023年6月15日 14:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479701.html
匿名

发表评论

匿名网友

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

确定