Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created December 23, 2023 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/5a2af65f8201202102b820702f6d38dc to your computer and use it in GitHub Desktop.
Save codeforfun-jp/5a2af65f8201202102b820702f6d38dc to your computer and use it in GitHub Desktop.
How to Create Dialog Series Datepicker - Kotlin
class MyDialogFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = activity?.let {
// デフォルトの日付を用意
val c = Calendar.getInstance()
val year = c[Calendar.YEAR]
val month = c[Calendar.MONTH]
val day = c[Calendar.DAY_OF_MONTH]
DatePickerDialog(it, this, year, month, day)
}
return dialog ?: throw IllegalStateException("アクティビティがNullです。")
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, dayOfMonth: Int) {
val msg = "${year}年${(month+1)}月${dayOfMonth}日"
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