1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jetpack Compose TextField フォーカスでキーボードを開閉する

Last updated at Posted at 2022-06-27

フォーカスを当てる

FocusRequester を使います。

val focusRequester = remember { FocusRequester() }

focusRequester を TextFiled に仕込みます。

TextField(
  modifier = Modifier.focusRequester(focusRequester)
)

それを Button クリックでフォーカスします。

Button(
  onClick = { focusRequester.requestFocus() }
) {

同時に、キーボードは開きます。

フォーカスを外す

同様に、FocusRequester でやれると思ったら、できません。

LocalFocusManager を使います。

val focusManager = LocalFocusManager.current

フォーカスを外してくれます。

同様に、ボタンに仕込みます。

Button(
  onClick = { focusManager.clearFocus() }
) {

これも、フォーカスを外すと同時にキーボードが閉じます。

screencapture-1656359742016.gif

👉 【Jetpack Compose】TextField の フォーカス と IME 開閉 と カーソル位置

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?