Android DatePickerDialog日期重置

时间:2011-03-14 09:45:06

标签: android dialog datepicker

我在活动中使用以下代码来创建DatePickerDialog。我想显示调用第一个活动时的当前日期。但它工作正常。但之后,对于某些特定情况,我想展示一些其他日期。如何重置DatePickerDialog日期?

// the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener ButtonTestDateListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    text_testdate.setText(
                            new StringBuilder()
                                    // Month is 0 based so add 1
                                    .append(formatter.format(mMonth + 1)).append("-")
                                    .append(formatter.format(mDay)).append("-")
                                    .append(mYear).append(" "));
                }
   };

   @Override
   protected Dialog onCreateDialog(int id) {
       switch (id) {
        case 0:
            return new DatePickerDialog(getParent(), ButtonTestDateListener, mYear, mMonth, mDay);
        }
        return null;
   }

1 个答案:

答案 0 :(得分:3)

使用datePickerDialog.updateDate(mYear, mMonth, mDay);