Home > other >  Android Jetpack Compose: Listen to user's keyboard input not working with Enter key
Android Jetpack Compose: Listen to user's keyboard input not working with Enter key

Time:10-01

I am trying to execute a function upon Enter Key pressed on soft keyboard and I enter image description here

with this option below in TextField

keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)

when I print logs on any key pressed, all other keys press are recognized but those 'Done' and '.-' keys.

CodePudding user response:

You can handle it with keyboardActions text field argument:

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

CodePudding user response:

The enter key is special. Especially when it's not an enter key, but a Done button or similar. Instead of sending a commitText, it send it via the OnEditorActionListener of the view. You need to set an OnEditorActionListener and handle the case in that.

  • Related