Help us understand the problem. What is going on with this article?

TimePickerの実装方法

More than 1 year has passed since last update.

Screenshot_20190626-113042_WeekCkech.jpg

Android Developerの説明

https://developer.android.com/reference/android/widget/TimePicker

ここから
https://developer.android.com/guide/topics/ui/controls/pickers#TimePicker

To display a TimePickerDialog using DialogFragment, you need to define a fragment class that extends DialogFragment and return a TimePickerDialog from the fragment's onCreateDialog() method.

TimePickerDialog(TimePickerのダイアログ、つまり画面)を表示するには DialogFragmentを使います。DialogFragmentを継承したフラグメントクラスを定義し、そのフラグメントクラス(DialogFragmentを継承したTimePickerDialog)のonCreateDialogの中でTimePickerDialigを返す必要があります。

実装

また、実装は
https://developer.android.com/guide/topics/ui/controls/pickers#TimePickerFragment

Define the onCreateDialog() method to return an instance of TimePickerDialog
Implement the TimePickerDialog.OnTimeSetListener interface to receive a callback when the user sets the time.

onCreateDialog()メソッドを定義してTimePickerDialogのインスタンスを返す
TimePickerDialog.OnTimeSetListenerインターフェイスを実装して、ユーザーが時刻を設定したときにコールバックを受け取る

イメージ

class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        // Use the current time as the default values for the picker
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)

        // Create a new instance of TimePickerDialog and return it
        return TimePickerDialog(activity, this, hour, minute, DateFormat.is24HourFormat(activity))
    }

    override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
        // Do something with the time chosen by the user
    }
}

表示

DialogFragment#showで表示

fun showTimePickerDialog(v: View) {
    TimePickerFragment().show(supportFragmentManager, "timePicker")
}

インナークラスで実装する

わざわざ、新規ファイルを作成する必要もない。自分がやったのは、ActivityではなくFragment内でTimePickerを扱った。(ViewPagerなどでよく使う)

AddEditTaskActivity.kt
class AddEditTaskActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_edit_task)
        ..........
    }
}
AddEditTaskFragment.kt
class AddEditTaskFragment : Fragment(), AddEditTaskContract.View {
    private fun showTimePicker() {
        class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

            override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
                // Use the current time as the default values for the picker
                val c = Calendar.getInstance()
                val hour = c.get(Calendar.HOUR_OF_DAY)
                val minute = c.get(Calendar.MINUTE)


                // Create a new instance of TimePickerDialog and return it
                return TimePickerDialog(activity, this, hour, minute, DateFormat.is24HourFormat(activity))
            }

            override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
                setLimitTime("$hourOfDay:$minute")
            }
        }

        TimePickerFragment().show((activity as FragmentActivity).fragmentManager, "TAG")
    }
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // OnClickのTimePicker
        limitTimeLayout.findViewById<TextView>(R.id.textView).setOnClickListener { showTimePicker() }
    }
}

はまった所

△(動いたり動かなかったりする)

AddEditTaskActivity.kt
class AddEditTaskActivity : AppCompatActivity() {
AddEditTaskFragment.kt
TimePickerFragment().show(activity?.fragmentManager, "TAG")
// OR TimePickerFragment().show((activity as FragmentActivity).fragmentManager, "TAG")

AddEditTaskActivity.kt
class AddEditTaskActivity : FragmentActivity() {
AddEditTaskFragment.kt
TimePickerFragment().show(activity.fragmentManager, "TAG")

Android Deveploperの文章を見て欲しい

This method calls show() on a new instance of the DialogFragment defined above. The show() method requires an instance of FragmentManager and a unique tag name for the fragment.

Caution: If your app supports versions of Android lower than 3.0, be sure that you call getSupportFragmentManager() to acquire an instance of FragmentManager. Also make sure that your activity that displays the time picker extends FragmentActivity instead of the standard Activity class.

ehuthon-kd
大学生。「好きなことして、楽しく生きて(いく)」
https://ehu-151.hatenadiary.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした