-
-
Save codeforfun-jp/6bc101c1c41e0d6e2057252a832fa54e to your computer and use it in GitHub Desktop.
How to Create Dialog Series Timepicker - Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyDialogFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener { | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
val dialog = activity?.let { | |
// デフォルトの時刻を用意 | |
val c = Calendar.getInstance() | |
val hour = c[Calendar.HOUR_OF_DAY] | |
val minute = c[Calendar.MINUTE] | |
TimePickerDialog(it, this, hour, minute, true) | |
} | |
return dialog ?: throw IllegalStateException("アクティビティがNullです。") | |
} | |
override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) { | |
val msg = "${hourOfDay}時${minute}分" | |
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment