Home > Net >  <Jetpack Compose> How to show/hide keyboard inside AlertDialog?
<Jetpack Compose> How to show/hide keyboard inside AlertDialog?

Time:09-22

Does anyone knows how can I show or hide the keyboard inside AlertDialog? The focusManager.clearFocus() doesn't work inside AlertDialog. Same for textInputService?.hideSoftwareKeyboard() and softwareKeyboardController?.hide().

For example:

AlertDialog(
    onDismissRequest = {
        openDialog.value = false
    },
    text = {
        TextField(...)
    }
    buttons = {
        Button(
            modifier = Modifier.fillMaxWidth(),
            onClick = { focusManager.clearFocus() }
        ) {
            Text("Update")
        }
    }
)

CodePudding user response:

The AlertDialog has its own LocalFocusManager as well as some other local constants.

Most likely you are capturing the LocalFocusManager outside of AlertDialog, instead you need to capture it inside:

buttons = {
    val focusManager = LocalFocusManager.current
    Button(
        modifier = Modifier.fillMaxWidth(),
        onClick = { focusManager.clearFocus() }
    ) {
        Text("Update")
    }
}
  • Related