我有这样的OutlinedTextField
OutlinedTextField(
value = value,
onValueChange = {
value = it
},
keyboardActions = KeyboardActions(
onDone = {
onResult(value)
}
),
modifier = Modifier
.fillMaxWidth()
.height(26.dp)
.focusRequester(focusRequester),
singleLine = true,
maxLines = 1
)
下面,我使用了BaseTextField我只是像这样设置了keyboardActions
// If color is not provided via the text style, use content color as a default
val textColor = textStyle.color.takeOrElse {
colors.textColor(enabled).value
}
val mergedTextStyle = textStyle.merge(TextStyle(color = textColor))
BasicTextField(
value = value,
onValueChange = {
if (it.text.length <= maxLength) {
onValueChange(it.text)
}
},
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
)
我怎么能抓住'输入键'与OutlinedTextField?我将它设置为"完成"动作。done不会被调用键盘也不会消失
你能不能试试这样做:
OutlinedTextField(
value = text, onValueChange = { text = it },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
println("done called")
})
)
参考链接:键盘动作
val showKeyboard = remember { mutableStateOf(true) }
val focusRequester = remember { FocusRequester() }
val keyboard = LocalSoftwareKeyboardController.current
OutlinedTextField(
value = text, onValueChange = { text = it },
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
println("done called")
}),
modifier = Modifier.onKeyEvent {
if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER){
focusRequester.requestFocus()
true
}
false
}
)
LaunchedEffect(focusRequester) {
if (showKeyboard.equals(true)) {
focusRequester.requestFocus()
delay(100) // Make sure you have delay here
keyboard?.show()
}
}