Skip to content

Instantly share code, notes, and snippets.

@satoshun
Last active December 18, 2022 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoshun/63cfeccd24727c58c767d357eac4d660 to your computer and use it in GitHub Desktop.
Save satoshun/63cfeccd24727c58c767d357eac4d660 to your computer and use it in GitHub Desktop.
Jetpack Compose キーボード少しめも

LocalSoftwareKeyboardController, FocusRequester の2クラスを使う

キーボードのshow/hide

LocalSoftwareKeyboardControllerを取得して、show/hideメソッドをコールする

@Composable
fun sample() {
  val keyboardController = LocalSoftwareKeyboardController.current
  ...
  
  // keyboardController.show()
  // keyboardController.hide()
}

compose時にキーボードを開く

FocusRequesterを使い、requestFocusメソッドでフォーカスを当てる

@Composable
fun sample() {
  val focusRequester = remember { FocusRequester() }

  LaunchedEffect(Unit) {
    focusRequester.requestFocus()
  }

  TextField(
    ...,
    modifier = Modifier.focusRequester(focusRequester)
  )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment