当键盘的 KeyboardType 设置为 KeyboardType.Number 时,我目前无法将用户输入捕获到文本字段中。
如果键盘设置为 KeyboardType.Text,文本字段会按预期更新,但是当设置为 KeyboardType.Number 时,文本字段无法更新。
这是为什么?以及如何更改我的代码,以便在单击文本字段时显示数字键盘,并在按下数字时更新文本字段中的相关数字。
以下代码不会更新文本字段(当设置为 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
)
}
非常感谢
答案 0 :(得分:0)
您应该更新 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
)
}