Michal Klimczak
11/27/2023, 8:41 AMAlexander Maryanovsky
11/27/2023, 9:47 AMModifier.onPreviewKeyEvent
Michal Klimczak
11/27/2023, 9:47 AMColumn {
//hide the selection control (the little drop that let's user reposition the cursor)
val customTextSelectionColors = TextSelectionColors(
handleColor = Color.Transparent,
backgroundColor = Color.Transparent
)
CompositionLocalProvider(
LocalTextInputService provides null, //don't show keyboard
LocalTextSelectionColors provides customTextSelectionColors
) {
var phoneNumber by remember { mutableStateOf(TextFieldValue("")) }
//request focus upon entering screen
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
BasicTextField(
modifier = Modifier.background(Color.LightGray).focusRequester(focusRequester),
value = phoneNumber,
onValueChange = { },
)
Button(onClick = {
val newText = phoneNumber.text + "1"
phoneNumber = TextFieldValue(newText, TextRange(newText.length)) //use TextField so that the cursor position is updated along with the text
}) {
Text(text = "1")
}
Button(onClick = {
val newText = phoneNumber.text.substring(0, max(phoneNumber.text.length - 1, 0))
phoneNumber = TextFieldValue(newText, TextRange(newText.length))
}) {
Text(text = "<")
}
}
}
Alexander Maryanovsky
11/27/2023, 9:48 AMMichal Klimczak
11/27/2023, 9:48 AMAlexander Maryanovsky
11/27/2023, 9:50 AMModifier.focusProperties {
canFocus = true
}
Alexander Maryanovsky
11/27/2023, 9:50 AM