Pregunta

I have a DatePicker in my activity. I want to set the year max limit to 2000, so that datepicker dont show years which is exceed year 2000. how can i implement this.currently i have used this code to set current date :

     dialog.getDatePicker().setMaxDate(new Date().getTime());

current work img :

enter image description here

I want look this :

enter image description here

this is my code :

protected Dialog onCreateDialog(int id) {
    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);
    switch (id) {
    case DATE_DIALOG_ID:
        // start changes...
        DatePickerDialog dialog = new DatePickerDialog(this,
                mDateSetListener, cyear, cmonth, cday);
        dialog.getDatePicker().setMaxDate(new Date().getTime());
        return dialog;
        // end changes...
    }
    return null;
}
¿Fue útil?

Solución

use this piece of code

protected Dialog onCreateDialog(int id) { Date d=new Date(); Calendar cal=Calendar.getInstance(); cal.set(2000, 1, 1, 0, 0); d.setTime(cal.getTimeInMillis()); Calendar c = Calendar.getInstance(); int cyear = c.get(Calendar.YEAR); int cmonth = c.get(Calendar.MONTH); int cday = c.get(Calendar.DAY_OF_MONTH); switch (id) { case DATE_DIALOG_ID: // start changes... DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday); dialog.getDatePicker().setMaxDate(d.getTime()); return dialog; // end changes... } return null; }

Otros consejos

You set current date that's why you are seeing 2014.

You can set bounds of your DatePicker by:

setMinDate(long minDate)
setMaxDate(long maxDate)

How you are going to set the values is explained by MH. in the topic below in details:

Set Limit on the DatePickerDialog in Android?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top