[英]Jetpack Compose: Adding a character counter to BasicTextField
I am trying to add a character counter inside a BasicTextField (I cant use TextField. It has to be below the Material level).我正在尝试在 BasicTextField 中添加一个字符计数器(我不能使用 TextField。它必须低于材料级别)。 I added a BasicText in the decorator but now I need to update it whenever someone changes the text.
我在装饰器中添加了一个 BasicText,但现在每当有人更改文本时我都需要更新它。 How do I get the text string from the innerTextField and update my BasicText?
如何从 innerTextField 获取文本字符串并更新我的 BasicText?
@Composable
fun MyTextField(
value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
modifier: Modifier = Modifier
){
val charCount = 0 // How do I update this in onValueChange?
BasicTextField(
value = value,
onValueChange = onValueChange,
modifier = modifier,
decorationBox = { innerTextField ->
Row(
Modifier
.padding(16.dp)
) {
BasicText(text = charCount.toString())
Spacer(Modifier.width(5.dp))
innerTextField()
}
})
}
You can simply use val charCount = value.text.count()
.您可以简单地使用
val charCount = value.text.count()
。 It will always be up to date thanks to recomposition happening automatically.由于自动进行重组,它将始终是最新的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.