Android Compose: Is there any way to dismiss dialog on click of “Done” key from keyboard
Asked Answered
W

1

5

I'm new to Android compose. Is there any way to dismiss dialog on click of Done key from keyboard using ImeAction?

Currently below code is clearfocus on click of Done along with how to dismiss the dialog:

    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        singleLine = true,
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = { focusRequester.requestFocus() }
        ),
        modifier = Modifier.onKeyEvent {
            if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER){
                focusRequester.requestFocus()
                true
            }
            false
        }
    )
Wrongdoer answered 15/10, 2022 at 10:8 Comment(5)
To dismiss the alert dialog?, if not please attach a screenshot for more clarity.Apologetic
What kind of dialog?Lyckman
there is a custom password dialog I need to dismiss onclick of onDone click which is ImeActionWrongdoer
Then you are probably managing the state of that dialog with MutableState<Boolean>. In that case just set that to false, or call the method you call to hide the dialog inside onDone method. Refer to the answer below.Apologetic
Check Thracian's answer.Lyckman
J
6

Inside onDone set the flag you use to show dialog to false

var showDialog by remember {mutableStateOf(false)}

 onDone = { 
    focusRequester.requestFocus()
    showDialog = false
 }

if(showDialog) {
   AlerDialog(...)
}

You check out the link below to show dialogs

Show custom alert dialog in Jetpack Compose

Jethro answered 15/10, 2022 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.