在抽屉打开时隐藏软键盘 - Jetpack Compose

Hide soft Keyboard on Drawer open - Jetpack Compose

我在撰写屏幕中有一个 TextField 和一个 ModalDrawer。我想在用户打开抽屉时关闭软键盘,但我一直无法弄清楚如何。在 ModalDrawer afaik 中没有触发 onOpened 生命周期事件。

您可以在 rememberDrawerState() 中使用 confirmStateChange 参数,并在 drawerValue 变为 DrawerValue.open 时调用 keyboardController.hide(),如下所示:

val keyboardController = LocalSoftwareKeyboardController.current
val state = rememberDrawerState(
  initialValue = DrawerValue.Closed,
    confirmStateChange = {
       if (it == DrawerValue.Open) {
           keyboardController?.hide()
       }
       true
    }
)

ModalDrawer(
   drawerState = state,
   ...
) {
  ...
}