gpt4 book ai didi

android - Jetpack 将数字输入输入到 TextField

转载 作者:行者123 更新时间:2023-12-04 23:47:23 24 4
gpt4 key购买 nike

当键盘的 KeyboardType 设置为 KeyboardType.Number 时,我目前无法将用户输入捕获到文本字段中。
如果键盘设置为 KeyboardType.Text,Textfield 会按预期更新,但是当设置为 KeyboardType.Number 时,Textfield 无法更新。
为什么是这样?以及如何更改我的代码,以便在单击文本字段时显示数字键盘,并且当按下数字时,在文本字段中更新相关数字。
以下代码不会更新文本字段(当设置为 KeyboardType.Number 时)...

@Composable
fun MyNumberField() {

var text = remember { mutableStateOf("")}

val change : (String) -> Unit = { it ->
value.value = it
}

TextField(
value = text.value,
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
onValueChange = change
)

}

以下代码确实更新了文本字段(当设置为 KeyboardType.Text 时)...
@Composable
fun MyNumberField() {

var text = remember { mutableStateOf("")}

val change : (String) -> Unit = { it ->
text.value = it
}

TextField(
value = value.value,
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Text),
onValueChange = change
)

}

非常感谢

最佳答案

您应该更新 text.value ,而不是 value.value您的代码中有错字,请将其更改为此。

@Composable
fun MyNumberField() {

var text = remember { mutableStateOf("")}

val change : (String) -> Unit = { it ->
value.value = it // you have this which is not correct and I don't think it even compiled
text.value = it // it is supposed to be this
}

TextField(
value = text.value,
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
onValueChange = change
)

}

关于android - Jetpack 将数字输入输入到 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67687674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com