Home > OS >  How to restrict Keyboard Enter key not to allow input in the next line (TextField in Android Jetpack
How to restrict Keyboard Enter key not to allow input in the next line (TextField in Android Jetpack

Time:11-04

In Android emulator, entering input using Computer Keyboard, but the "Enter" key on my keyboard should take the input and do the action. Instead, the input is allowing me in the next line, and keep on continuing to the next line (as a new line character). Please suggest me your answers in Android Jetpack Composable of TextField element.

CodePudding user response:

You can use something like:

TextField(
    value = text,
    onValueChange = {text = it},
    singleLine = true ,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { /* do something */}
    ),
    modifier = Modifier.onKeyEvent {
        if (it.key == Key.Enter){
            /* do something */
            true
        }
        false
    }
)

CodePudding user response:

Hey you can use singleLine = true in text field, To perform any custom actions on click of Done/Enter Key in keyboard you can use something like

    TextField(
    value = text,
    onValueChange = {text = it},
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { /* do something */},
    onGo = { /* do something */},
    onNext = { /* do something */},
    onPrevious = { /* do something */},
    onSearch = { /* do something */},
    onSend = { /* do something */})
)
  • Related