To place the cursor at the end of the text, you must use TextFieldValue
The following code, give the focus to the TextField and place the cursor at the end of the text :
@Composable
fun TestTextField(title: String = "Hello") {
val titleFocusRequester = remember { FocusRequester() }
val titleTextFieldValueState = remember {
mutableStateOf(
TextFieldValue(
text = title,
selection = TextRange(title.length)
)
)
}
LaunchedEffect(Unit) {
titleFocusRequester.requestFocus()
}
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
TextField(
modifier = Modifier
.fillMaxWidth()
.focusRequester(titleFocusRequester),
value = titleTextFieldValueState.value,
onValueChange = { tfv: TextFieldValue ->
titleTextFieldValueState.value = tfv
}
)
}
}
* Be the first to Make Comment