1

这个问题描述了如何使用 DatePicker 小部件来做到这一点,但我没有这样的东西。

我有这些:

@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;
    }
}

然后是三个DatePickerDialog.OnDateSetListener() s

这些 DatePickerDialogs 上没有 setMax() 或 setMin()。

提前致谢。

4

2 回答 2

9

只需实现您自己的 DatePickerDialog,如果用户尝试将其更改为低于最小日期,它将日期更改回允许的日期。

我正在使用的剥离版本是:

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;
}   

}

于 2012-05-31T15:28:38.357 回答
0

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));
            }

        }
    });
于 2012-08-13T10:18:27.700 回答