在当前日期时间之后设置Datepicker minDate会在DatePickerDialog的顶部添加一个今天显示的文本

时间:2016-06-28 12:57:56

标签: android datepicker

如果datepicker的最小日期早于当前日期时间,则DatepickerDialog视图正常。 (第一次截图)

Calendar startDateCalendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, null, startDateCalendar
        .get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH),
        startDateCalendar.get(Calendar.DAY_OF_MONTH));
DatePicker datePicker = datePickerDialog.getDatePicker();
Calendar max = Calendar.getInstance();
datePicker.setMinDate(max.getTimeInMillis() - 100);
datePickerDialog.show();

如果datepicker的最小日期是在当前日期时间之后,则DatepickerDialog视图会在今天显示的视图顶部添加文本视图。 (第二个截图)

Calendar startDateCalendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, null, startDateCalendar
        .get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH),
        startDateCalendar.get(Calendar.DAY_OF_MONTH));
DatePicker datePicker = datePickerDialog.getDatePicker();
Calendar max = Calendar.getInstance();
datePicker.setMinDate(max.getTimeInMillis() + 100);
datePickerDialog.show();

enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

使用此代码:

DatePicker datePicker = datePickerDialog.getDatePicker();
datePicker.setMinDate(System.currentTimeMillis() + 100);
datePickerDialog.setTitle(null);
datePickerDialog.show();

快速解释:

  • setMinDate设置对话框标题,我们需要取消设置

  • 使用System.currentTimeMillis()比调用更复杂的Calendar.getInstance()更有效