如何禁用最近3天和将来的DateTimePicker Android

时间:2018-08-10 02:13:25

标签: java android datetimepicker datepickerdialog

我尝试首次使用DateTimePicker,但在过去三天及以后的日子里无法使用,我感到困惑。嗯,也许我一直在研究http://tips.androidgig.com/disable-future-dates-from-datepickerdialog/ 但是我无法在我的函数中实现。

这是我单击TextView时显示DateTimePicker的功能

private void setDatePicker() {
calDate1 = Calendar.getInstance();
calDate2 = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateSet1 = new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker datePicker, int year, int month, int date) {
    calDate1.set(year, month, date);
    displayDate(tvDate1, calDate1);
  }
};
tvDate1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    new DatePickerDialog(TicketStatusActivity.this, dateSet1, calDate1.get(Calendar.YEAR), calDate1.get(Calendar.MONTH), calDate1.get(Calendar.DAY_OF_MONTH)).show();
  }
});
final DatePickerDialog.OnDateSetListener dateSet2 = new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker datePicker, int year, int month, int date) {
    calDate2.set(year, month, date);
    displayDate(tvDate2, calDate2);
  }
};
tvDate2.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    new DatePickerDialog(TicketStatusActivity.this, dateSet2, calDate2.get(Calendar.YEAR), calDate2.get(Calendar.MONTH), calDate2.get(Calendar.DAY_OF_MONTH)).show();
  }
});
displayDate(tvDate1, calDate1);
displayDate(tvDate2, calDate2);}

很抱歉,这个问题不重要,我的语法不好 고맙습니다

1 个答案:

答案 0 :(得分:1)

在日期选择器中设置MaxDate。

尝试一下:

Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -3);
        DatePickerDialog dpDialog = new DatePickerDialog(this, myDateListener, mYear, mMonth, mDay);
        dpDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());