日期和时间选择器对话框

Posted

技术标签:

【中文标题】日期和时间选择器对话框【英文标题】:Date & Time Picker Dialog 【发布时间】:2021-08-11 15:01:31 【问题描述】:

我正在尝试通过对开始和结束时间使用单独的日期和时间选择器来设置事件的日期和时间。

开始日期和开始时间 结束日期和结束时间

我整理的代码更新了日期,但时间没有改变

任何人都可以就这个问题提供指导吗?

谢谢

代码如下:

private val START_CAL_ID = 456
private lateinit var endCalendar: Calendar
private val END_CAL_ID = 678
private lateinit var endTimeCalendar: Calendar

private val 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)

【问题讨论】:

【参考方案1】:

set(int,int) in Calendar 在其第一个参数中接收字段 id,在第二个参数中接收值。要设置小时和分钟,请将其拆分为两个调用,例如

startCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
startCalendar.set(Calendar.MINUTE, minute)

(另外,请考虑改用 java.time API。它们更易于使用。)

【讨论】:

【参考方案2】:

您可以使用单独的帮助类来获取时间和日期,以获得更好、更简洁的代码。

日期选择器助手:

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 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)
    

最后是片段或活动中使用的时间选择器,如下所示:

 JustTimePicker(context!!,object : JustTimePicker.TimePickerInterface
                    override fun onTimeSelected(calendar: Calendar) 
                     val time =       calendar.time
                        //todo
                    
                ).showPicker()

【讨论】:

以上是关于日期和时间选择器对话框的主要内容,如果未能解决你的问题,请参考以下文章

Android:时间选择器和日期选择器在同一个对话框中

android-时间选择器和日期选择器

一个对话框中的 Android 日期时间选择器

日期选择器和时间选择器的问题

日期/时间选择器不提供更改的值

如何在日期选择器对话框中设置日期限制