class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c: Calendar = Calendar.getInstance()
val year: Int = c.get(Calendar.YEAR)
val month: Int = c.get(Calendar.MONTH)
val day: Int = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(getActivity()!!, this, year, month, day)
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
val tv: TextView = activity!!.findViewById(R.id.txt_date_picker) as TextView
tv.setText("$day - $month - $year")
}
override fun onCancel(dialog: DialogInterface?) {
Toast.makeText(activity,"date picker Canceled.", Toast.LENGTH_SHORT).show()
super.onCancel(dialog)
}
}
Редактировать: Весь код после попытки ответа:
class TimePickerFragment: DialogFragment(), TimePickerDialog.OnTimeSetListener{
private lateinit var calendar:Calendar
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
return TimePickerDialog(
activity, // Context
android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth, // Theme
this, // TimePickerDialog.OnTimeSetListener
hour, // Hour of day
minute, // Minute
false // Is 24 hour view
)//.apply {
// getButton(DialogInterface.BUTTON_NEGATIVE).setText(R.string.myCancelString)
// }
}
override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
// Do something with the returned time
val tv:TextView = activity.findViewById(R.id.end) as TextView
// tv.text = "${getHourAMPM(hourOfDay)}:$minute ${getAMPM(hourOfDay)}"
tv.text = "$hourOfDay:$minute "
}
}