日期和时间选择器对话框
Date & Time Picker Dialog
我正在尝试通过为开始时间和结束时间使用单独的日期和时间选择器来设置事件的日期和时间。
开始日期和开始时间
结束日期和结束时间
我放在一起的代码更新了日期,但时间没有改变
任何人都可以指导这个问题吗?
谢谢
代码如下:
private val START_CAL_ID = 456
private lateinit var endCalendar: Calendar
private val END_CAL_ID = 678
private lateinit var endTimeCalendar: Calendar
私人日历 = Calendar.getInstance()
点击日期显示日期选择器,点击时间显示时间选择器
private fun showCalendar(calendarId: Int) {
val datePicker = DatePickerDialog(
this,
R.style.DateTimePickerTheme,
{ datePicker: DatePicker, year: Int, month: Int, day: Int ->
if (calendarId == START_CAL_ID) {
startCalendar.set(year, month, day)
} else if (calendarId == END_CAL_ID) {
endCalendar.set(year, month, day)
}
updateDateFields()
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
)
datePicker.show()
}
private fun showTime(calendarId: Int) {
val timePickerDialog = TimePickerDialog(
this, R.style.DateTimePickerTheme,
{ view: TimePicker?, hourOfDay: Int, minute: Int ->
if (calendarId == START_CAL_ID) {
startCalendar.set(hourOfDay, minute)
} else if (calendarId == END_CAL_ID) {
endCalendar.set(hourOfDay, minute)
}
updateTimeFields()
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false
)
timePickerDialog.show()
}
更新应用中的字段以正确格式显示所选数据
private fun updateDateFields() {
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.getDefault())
findViewById<TextView>(R.id.editEventStartDate).text = formatter.format(startCalendar.time)
findViewById<TextView>(R.id.editEventEndDate).text = formatter.format(endCalendar.time)
}
private fun updateTimeFields() {
val timeFormatter = SimpleDateFormat("hh.mm a", Locale.getDefault())
findViewById<TextView>(R.id.editEventStartTime).text =
timeFormatter.format(startCalendar.time)
findViewById<TextView>(R.id.editEventEndTime).text = timeFormatter.format(endCalendar.time)
}
set(int,int)
in Calendar
在第一个参数中接收字段 id,在第二个参数中接收值。要设置小时和分钟,将其拆分为两个调用,例如
startCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
startCalendar.set(Calendar.MINUTE, minute)
(另外,请考虑改用 java.time API。它们更易于使用。)
您可以使用单独的帮助程序 classes 来获取时间和日期以获得更好、更干净的代码。
日期选择器助手:
class DatePicker(var context: Context, var iface : DateTimePickerInterface?) :
DatePickerDialog.OnDateSetListener {
val selectedDate = Calendar.getInstance()
fun showDatePicker(){
val datePickerDialog = DatePickerDialog(
context,
R.style.DateTimePickerTheme,
this,
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DAY_OF_MONTH)
)
datePickerDialog.show()
}
override fun onDateSet(p0: DatePicker?, year: Int, month: Int, day: Int) {
selectedDate.set(Calendar.YEAR,year)
selectedDate.set(Calendar.MONTH,month)
selectedDate.set(Calendar.DAY_OF_MONTH,day)
iface?.onDateTimeSelected(selectedDate)
}
interface DateTimePickerInterface {
fun onDateTimeSelected(calendar: Calendar)
}
}
您可以像这样在 Activity 或 Fragment 中使用它:
JustDatePicker(this@Activity,lang.toString(),object : JustDatePicker.DateTimePickerInterface {
override fun onDateTimeSelected(calendar: Calendar) {
val date : Date = calendar.time
//todo
}
}).showDatePicker()
与 TimePicker 类似 class。
class JustTimePicker(var context: Context, var iface : TimePickerInterface?,var selectedTime: Calendar = Calendar.getInstance()) : TimePickerDialog.OnTimeSetListener {
fun showPicker(){
val timePickerDialog = TimePickerDialog(
context,
this,
selectedTime.get(Calendar.HOUR_OF_DAY),
selectedTime.get(Calendar.MINUTE),
true
)
timePickerDialog.show()
}
override fun onTimeSet(p0: TimePicker?, hour: Int, min: Int) {
selectedTime.set(Calendar.HOUR_OF_DAY,hour)
selectedTime.set(Calendar.MINUTE,min)
iface?.onTimeSelected(selectedTime)
}
interface TimePickerInterface {
fun onTimeSelected(calendar: Calendar)
}
}
最后在片段或 activity 中使用时间选择器,如下所示:
JustTimePicker(context!!,object : JustTimePicker.TimePickerInterface{
override fun onTimeSelected(calendar: Calendar) {
val time = calendar.time
//todo
}
}).showPicker()
我正在尝试通过为开始时间和结束时间使用单独的日期和时间选择器来设置事件的日期和时间。
开始日期和开始时间 结束日期和结束时间
我放在一起的代码更新了日期,但时间没有改变
任何人都可以指导这个问题吗?
谢谢
代码如下:
private val START_CAL_ID = 456
private lateinit var endCalendar: Calendar
private val END_CAL_ID = 678
private lateinit var endTimeCalendar: Calendar
私人日历 = Calendar.getInstance()
点击日期显示日期选择器,点击时间显示时间选择器
private fun showCalendar(calendarId: Int) {
val datePicker = DatePickerDialog(
this,
R.style.DateTimePickerTheme,
{ datePicker: DatePicker, year: Int, month: Int, day: Int ->
if (calendarId == START_CAL_ID) {
startCalendar.set(year, month, day)
} else if (calendarId == END_CAL_ID) {
endCalendar.set(year, month, day)
}
updateDateFields()
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
)
datePicker.show()
}
private fun showTime(calendarId: Int) {
val timePickerDialog = TimePickerDialog(
this, R.style.DateTimePickerTheme,
{ view: TimePicker?, hourOfDay: Int, minute: Int ->
if (calendarId == START_CAL_ID) {
startCalendar.set(hourOfDay, minute)
} else if (calendarId == END_CAL_ID) {
endCalendar.set(hourOfDay, minute)
}
updateTimeFields()
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false
)
timePickerDialog.show()
}
更新应用中的字段以正确格式显示所选数据
private fun updateDateFields() {
val formatter = SimpleDateFormat("MMM dd, yyyy", Locale.getDefault())
findViewById<TextView>(R.id.editEventStartDate).text = formatter.format(startCalendar.time)
findViewById<TextView>(R.id.editEventEndDate).text = formatter.format(endCalendar.time)
}
private fun updateTimeFields() {
val timeFormatter = SimpleDateFormat("hh.mm a", Locale.getDefault())
findViewById<TextView>(R.id.editEventStartTime).text =
timeFormatter.format(startCalendar.time)
findViewById<TextView>(R.id.editEventEndTime).text = timeFormatter.format(endCalendar.time)
}
set(int,int)
in Calendar
在第一个参数中接收字段 id,在第二个参数中接收值。要设置小时和分钟,将其拆分为两个调用,例如
startCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
startCalendar.set(Calendar.MINUTE, minute)
(另外,请考虑改用 java.time API。它们更易于使用。)
您可以使用单独的帮助程序 classes 来获取时间和日期以获得更好、更干净的代码。
日期选择器助手:
class DatePicker(var context: Context, var iface : DateTimePickerInterface?) :
DatePickerDialog.OnDateSetListener {
val selectedDate = Calendar.getInstance()
fun showDatePicker(){
val datePickerDialog = DatePickerDialog(
context,
R.style.DateTimePickerTheme,
this,
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DAY_OF_MONTH)
)
datePickerDialog.show()
}
override fun onDateSet(p0: DatePicker?, year: Int, month: Int, day: Int) {
selectedDate.set(Calendar.YEAR,year)
selectedDate.set(Calendar.MONTH,month)
selectedDate.set(Calendar.DAY_OF_MONTH,day)
iface?.onDateTimeSelected(selectedDate)
}
interface DateTimePickerInterface {
fun onDateTimeSelected(calendar: Calendar)
}
}
您可以像这样在 Activity 或 Fragment 中使用它:
JustDatePicker(this@Activity,lang.toString(),object : JustDatePicker.DateTimePickerInterface {
override fun onDateTimeSelected(calendar: Calendar) {
val date : Date = calendar.time
//todo
}
}).showDatePicker()
与 TimePicker 类似 class。
class JustTimePicker(var context: Context, var iface : TimePickerInterface?,var selectedTime: Calendar = Calendar.getInstance()) : TimePickerDialog.OnTimeSetListener {
fun showPicker(){
val timePickerDialog = TimePickerDialog(
context,
this,
selectedTime.get(Calendar.HOUR_OF_DAY),
selectedTime.get(Calendar.MINUTE),
true
)
timePickerDialog.show()
}
override fun onTimeSet(p0: TimePicker?, hour: Int, min: Int) {
selectedTime.set(Calendar.HOUR_OF_DAY,hour)
selectedTime.set(Calendar.MINUTE,min)
iface?.onTimeSelected(selectedTime)
}
interface TimePickerInterface {
fun onTimeSelected(calendar: Calendar)
}
}
最后在片段或 activity 中使用时间选择器,如下所示:
JustTimePicker(context!!,object : JustTimePicker.TimePickerInterface{
override fun onTimeSelected(calendar: Calendar) {
val time = calendar.time
//todo
}
}).showPicker()