这是我的@Composable@Composablefun CenteredTextField(text:String,onTextChange:(String)-> Unit,modifier:Modifier = Modifier){val focusRequester = 记住{FocusRequester(...
这是我的@Composable
@Composable
fun CenteredTextField(
text: String, onTextChange: (String) -> Unit, modifier: Modifier = Modifier
) {
val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
val textFieldValue = remember { mutableStateOf(TextFieldValue(text)) }
LaunchedEffect(Unit) {
textFieldValue.value = textFieldValue.value.copy(
selection = TextRange(textFieldValue.value.text.length) //
)
focusRequester.requestFocus() //
keyboardController?.show() //
}
Box(
contentAlignment = Alignment.Center, modifier = modifier.fillMaxWidth() // Box
) {
OutlinedTextField(
value = textFieldValue.value,
onValueChange = {
textFieldValue.value = it
onTextChange(it.text)
},
modifier = Modifier.align(Alignment.Center) // TextField
.widthIn(min = 100.dp) //
.padding(horizontal = 16.dp).focusRequester(focusRequester), // TextField
textStyle = TextStyle(textAlign = TextAlign.Center) //
)
}
}
第一个显示,光标不集中显示。你必须输入一个单词。我知道不使用 TextFieldValue
可以解决。当回显时,我希望光标在文本的末尾。我需要帮助来解决。
我找不到任何解决方法。