3
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 5 years have passed since last update.

【Android】AlertDialogでNumberPickerを使うときはフォーカスを外す処理を入れる

Posted at

AlertDialogでNumberPickerを使う

picker_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="horizontal"
        android:gravity="center"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:text="なんかの文字"
            android:textSize="32sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <NumberPicker
            android:id="@+id/picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>
PickerDialog.kt
private lateinit var dialogView: View
var pickerNum: Int = 0
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.picker_dialog, null)
    dialogView.picker.setOnValueChangedListener { _, _, newVal -> pickerNum = newVal }

    return AlertDialog.Builder(requireContext())
        .setView(dialogView)
        .setPositiveButton(android.R.string.ok) { _, _ ->
            Log.d("PickerDialog", "${pickerNum}")
        }
        .setNegativeButton(android.R.string.cancel, null)
        .create()
}

上記のようにしてNumberPickerを使うときの注意点です。

テキスト入力だとNumberPickerの値が反映されない

NumberPickerをドラムロールとして使う分には上記の構成でも問題ありません。OKボタンを押すとログに選択している値が表示されるという想定通りの動作となります。

しかし、NumberPickerをタップしてテキスト入力の場合に問題があります。

テキスト入力で値を設定し入力ダイアログが表示された状態でOKボタンを押しても、変更前の値のままになっているかと思います。入力ダイアログを閉じてからならば変更後の値になります。

NumberPickerのテキスト入力はフォーカスが外れたときに設定される

原因はNumberPickerのテキスト入力はフォーカスが外れたときに設定されるという動作になっているためです。

入力ダイアログが表示された状態ではNumberPicker(の中のEditText)にフォーカスした状態のままなので、値の設定が行われません。

そのため、

PickerDialog.kt
private lateinit var dialogView: View
var pickerNum: Int = 0
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.picker_dialog, null)
    dialogView.picker.setOnValueChangedListener { _, _, newVal -> pickerNum = newVal }

    return AlertDialog.Builder(requireContext())
        .setView(dialogView)
        .setPositiveButton(android.R.string.ok) { _, _ ->
            dialogView.clearFocus()
            Log.d("PickerDialog", "${pickerNum}")
        }
        .setNegativeButton(android.R.string.cancel, null)
        .create()
}

のようにしてOKボタンを押したときにフォーカスを外す処理を入れる必要があります。

3
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
3
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?