从特定日期启动DatePicker对话框

时间:2015-06-06 17:27:15

标签: android datepicker

我建立了一个日期选择器对话框,我希望从特定日期开始,并隐藏1995年以后的所有年份。我该怎么做?

我的代码:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_PICKER_ID:

        return new DatePickerDialog(this, pickerListener, year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.


    @Override
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {

        year  = selectedYear;
        month = selectedMonth;
        day   = selectedDay;

        // Show selected date 
        output.setText(new StringBuilder().append(year)
                .append("-").append(month + 1).append("-").append(day)
                .append(" "));


       }
    };

谢谢。

2 个答案:

答案 0 :(得分:1)

      Calendar c = Calendar.getInstance();
      int mYear = c.get(Calendar.YEAR);
      int mMonth = c.get(Calendar.MONTH);
      int mDay = c.get(Calendar.DAY_OF_MONTH);

         DatePickerDialog dialog =
        new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
        dialog.show();

答案 1 :(得分:0)

要在1995年之后隐藏所有内容,请使用JodaTime并设置最大日期,如下所示:

dialog.getDatePicker().setMaxDate(new DateTime(1995, 0, 0, 0, 0).getMillis());