Frage

This question describes how to do it using a DatePicker widget, but I haven't got anything like that.

I have these:

@Override
protected Dialog onCreateDialog(int id)
{

    switch (id)
    {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear1, mMonth1, mDay1);
    case DATE_DIALOG_ID_2:
        return new DatePickerDialog(this, mDateSetListener2, mYear2, mMonth2, mDay2);
    case DATE_DIALOG_ID_3:
        return new DatePickerDialog(this, mDateSetListener3, mYear3, mMonth3, mDay3);
    }
    return null;
}


protected void onPrepareDialog(int id, Dialog dialog)
{

    switch (id)
    {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(mYear1, mMonth1, mDay1);
        break;
    case DATE_DIALOG_ID_2:
        ((DatePickerDialog) dialog).updateDate(mYear2, mMonth2, mDay2);
        break;
    case DATE_DIALOG_ID_3:
        ((DatePickerDialog) dialog).updateDate(mYear3, mMonth3, mDay3);
        break;
    }
}

and then three DatePickerDialog.OnDateSetListener() s

There is no setMax() or setMin() on these DatePickerDialogs.

Thanks in advance.

War es hilfreich?

Lösung

Just implement your own DatePickerDialog, that changes the date back to a allowed date if the user tries to change it below the minimum date.

A stripped version of the one I am using is:

public class MyDatePickerDialog extends DatePickerDialog{

private Date maxDate;
private Date minDate;

public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
    super(context, callBack, year, monthOfYear, dayOfMonth);        
    init(year, monthOfYear, dayOfMonth);
}

public MyDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear,    int dayOfMonth) {
    super(context, theme, callBack, year, monthOfYear, dayOfMonth);
    init(year, monthOfYear, dayOfMonth);
}

private void init(int year, int monthOfYear, int dayOfMonth){
    Calendar cal = Calendar.getInstance();

    cal.set(1970, Calendar.JANUARY, 1);
    minDate = cal.getTime();

    cal.set(3000, Calendar.JANUARY, 1);
    maxDate = cal.getTime();

    cal.set(year, monthOfYear, dayOfMonth);
}

public void onDateChanged (final DatePicker view, int year, int month, int day){
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day);
    Date currentDate = cal.getTime();

    final Calendar resetCal = cal; 
    if(!minDate.before(currentDate) ){
        cal.setTime(minDate);
        view.updateDate(resetCal.get(Calendar.YEAR), resetCal.get(Calendar.MONTH), resetCal.get(Calendar.DAY_OF_MONTH));

    }else if(maxDate.before(currentDate)){
        cal.setTime(maxDate);
        view.updateDate(resetCal.get(Calendar.YEAR), resetCal.get(Calendar.MONTH), resetCal.get(Calendar.DAY_OF_MONTH));
    }       
}

public void setMaxDate(Date date){
    this.maxDate = date;
}

public void setMinDate(Date date){
    this.minDate = date;
}   

}

Andere Tipps

my first answer. I hope everything works. I have the same problem with the datepicker embedded in an acitivity. Based on this answer I got this solution but only for Maximum Date.

Works fine in the emulator also for ICS.

// Get the Date for INIT
Calendar c = Calendar.getInstance();
int INIT_JAHR = c.get(Calendar.YEAR);
int INIT_MONAT = c.get(Calendar.MONTH);
int INIT_TAG = c.get(Calendar.DAY_OF_MONTH);
this.dp_quellen_datum.init(INIT_JAHR, INIT_MONAT, INIT_TAG,
    new DatePicker.OnDateChangedListener() {

        public void onDateChanged(DatePicker datePicker,
                int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method sub
            Calendar cal = Calendar.getInstance();
            Calendar maxCal = Calendar.getInstance();
            // Actual date from datepicker
            cal.set(year, monthOfYear, dayOfMonth);
            // maximum Date in my Case today
            maxCal.set(maxCal.get(Calendar.YEAR),
                    maxCal.get(Calendar.MONTH),
                    maxCal.get(Calendar.DAY_OF_MONTH));
            Log.d("MAX -> Aktuell", maxCal.getTime()
                    + "-->"
                    + cal.getTime()
                    + "===>"
                    + maxCal.getTime()
                            .before(cal.getTime()));
            // Set datepicker to today if selected date in the feature
            final Calendar resetCal = cal; // <- Don't tested if this line is needed
            if (maxCal.getTime().before(cal.getTime()) == true) {
                cal.setTime(maxCal.getTime());
                Log.d("Anpassen", "AKTIV");
                datePicker.updateDate(
                        resetCal.get(Calendar.YEAR),
                        resetCal.get(Calendar.MONTH),
                        resetCal.get(Calendar.DAY_OF_MONTH));
            }

        }
    });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top