2
3

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.

Kotlinで時間を選ぶPickerを扱う

Posted at

このようなTimePickerのDialogを作る。
IMG_3532.JPG


DialogなのでDialogfragmentとかく。


class TimePicker: DialogFragment(), TimePickerDialog.OnTimeSetListener {

private var listener : onTimeSetListener? = null
override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is onTimeSetListener) {
        listener = context
    } else {
        throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
    }
}

override fun onDetach() {
    super.onDetach()
    listener = null
}

現在時刻を取得する。

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    super.onCreate(savedInstanceState)

    val calender = Calendar.getInstance()
    val hour = calender.get(Calendar.HOUR)
    val minute = calender.get(Calendar.MINUTE)

    return TimePickerDialog(activity, this, hour, minute, true)
}

選択した時刻を取得し、任意のSimpleDateFormatで表す。
選択していない年、月、日はゼロを設定する。


private fun getTimeString(hourOfDay: Int, minute: Int): String{
    val calendar = Calendar.getInstance()
    calendar.set(0,0,0,hourOfDay,minute)
    return SimpleDateFormat("HH : mm").format(calendar.time)
}

先ほど取得した時刻を元のActivityに返す。

override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {

    val time = getTimeString(hourOfDay,minute)
    listener?.onSelected(time)
    // onSelected は返すための関数。後述
}

Dialogを閉じる。

fragmentManager!!.beginTransaction().remove(this).commit()

interfaceをかく。

interface onTimeSetListener{
    fun onSelected(time: String)
}

元のActivityに以下の二つを書く。
classのところ↓

TimePicker.onTimeSetListener

Pickerで取得した時刻を得るため↓

override fun onSelected(time: String) {
}

###DatePickerの場合
上述のonCreateDialog, getDateString, onDateSetを下のように書き換える。

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    super.onCreate(savedInstanceState)

    val calender = Calendar.getInstance()
    val year = calender.get(Calendar.YEAR)
    val month = calender.get(Calendar.MONTH)
    val day = calender.get(Calendar.DAY_OF_MONTH)

    return DatePickerDialog(activity, this, year, month, day)
}

private fun getDateString(year: Int, month: Int, day: Int):String{
    val calendar = Calendar.getInstance()
    calendar.set(year, month, day)
    return SimpleDateFormat("yyyy / MM / dd EEE")
.format(calendar.time)
}

override fun onDateSet(datePicker: DatePicker?, year: Int, month: Int, day: Int) {
    val date = getDateString(year, month, day)
    listener?.onSelected(date)
}
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?