1

I have OutlinedTextField like so.

OutlinedTextField(
    value = value,
    onValueChange = {
        value = it
    },
    keyboardActions = KeyboardActions(
        onDone = {
            onResult(value)
        }
    ),
    modifier = Modifier
        .fillMaxWidth()
        .height(26.dp)
        .focusRequester(focusRequester),
    singleLine = true,
    maxLines = 1
)

under the hood, I used BaseTextField and I just set keyboardActions there like so.

// 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,
)

How can I catch 'enter key' with OutlinedTextField? I set it to 'Done' Action. And done is not called and also the keyboard doesn't disappear.

c-an
  • 3,543
  • 5
  • 35
  • 82

1 Answers1

0

Can you please try something like this:

OutlinedTextField(
  value = text, onValueChange = { text = it },
  keyboardOptions = KeyboardOptions.Default.copy(
     keyboardType = KeyboardType.Number,
     imeAction = ImeAction.Done
  ),
  keyboardActions = KeyboardActions(onDone = {
    println("done called")
 })
)

Reference link: Keyboard actions

UPDATED

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()
 }
}
inkredusk
  • 919
  • 4
  • 16
  • I changed it to it and keyboard doesn't show up and done is not called. – c-an Apr 04 '23 at 02:58
  • I fixed the keyboard visibility issue. But even though I press enter on my physical keyboard, 'done' is not called. But soft keybaord works. I need to force enter key. – c-an Apr 04 '23 at 03:52
  • You can try adding a `modifier` as well. Hopefully it should work. I have updated my code – inkredusk Apr 04 '23 at 03:56
  • Well, `focusRequester.requestFocus()` is just focusing it, not calling 'done' – c-an Apr 04 '23 at 04:07
  • Ohh. Sorry to hear that. Can you check. this link once https://stackoverflow.com/questions/64947249/jetpack-compose-setting-imeaction-does-not-close-or-change-focus-for-the-keyboa – inkredusk Apr 04 '23 at 09:05
  • I was looking for something like `keyboardActions.done`. It's not about focus. – c-an Apr 04 '23 at 09:11