3

目前我正在使用带有修改的本机对话框来隐藏日期微调器,并且工作正常。

但是现在我想以这样的格式设置日期,当我旋转月份微调器或年份微调器时,对话框只显示月份和年份而不是完整的日历日期,我的日期选择器对话框的自定义代码如下:

    @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date


        DatePickerDialog datePickerDialog = this.customDatePicker();
        return datePickerDialog;
    }
    return null;
}


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


    // when dialog box is closed, below method will be called.
    @Override
    public void onDateSet(DatePicker datepickerView, int selectedYear,
            int selectedMonth, int selectedDay) {


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

        mynewYear=selectedYear;
        myNewMonth=selectedMonth;
        myNewDay=selectedDay;

        try {
            checkDate();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*time_date_TV.setText(new StringBuilder()
          .append(myNewMonth + 1).append("/")
          .append(myNewDay ).append("/").append(mynewYear));
               */





    }
};

private DatePickerDialog customDatePicker() {

    DatePickerDialog dpd = new DatePickerDialog(context,  android.R.style.Theme_Holo_Light_Dialog,datePickerListener,
            year, month,day );
     if(month==0)
       {
           mmonth="January";
       }
       if(month==1)
       {
           mmonth="Faburary";
       }
       if(month==2)
       {
           mmonth="March";
       }
       if(month==3)
       {
           mmonth="April";
       }
       if(month==4)
       {
           mmonth="May";
       }
       if(month==5)
       {
           mmonth="June";
       }
       if(month==6)
       {
           mmonth="July";
       }
       if(month==7)
       {
           mmonth="August";
       }
       if(month==8)
       {
           mmonth="September";
       }if(month==9)
       {
           mmonth="October";
       }
       if(month==10)
       {
           mmonth="November";
       }
       if(month==11)
       {
           mmonth="December";
       }

    dpd.setTitle(""+mmonth+", "+year);
    try {

    Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
    for (Field datePickerDialogField : datePickerDialogFields) {
    if (datePickerDialogField.getName().equals("mDatePicker")) {
    datePickerDialogField.setAccessible(true);
    DatePicker datePicker = (DatePicker) datePickerDialogField
    .get(dpd);

    Field datePickerFields[] = datePickerDialogField.getType()
    .getDeclaredFields();
    for (Field datePickerField : datePickerFields) {
    if ("mDayPicker".equals(datePickerField.getName())
    || "mDaySpinner".equals(datePickerField
    .getName())) {
    datePickerField.setAccessible(true);
    Object dayPicker = new Object();
    dayPicker = datePickerField.get(datePicker);
    ((View) dayPicker).setVisibility(View.GONE);
    }
    }
    }

    }
    } catch (Exception ex) {
    }
    return dpd;
    }

当对话框第一次弹出时它工作正常,因为我设置了标题

DatePickerDialog.setTitle("");

但是当对话框第一次弹出时效果很好但我想更新ondatechangelistner所以我该怎么做

4

1 回答 1

2

经过半天的头脑风暴,我找到了这个解决方案,这可能有助于其他有这种情况的人

我的日期选择器自定义类如下,每次用户更改月/年时都会更新标题中的日期

class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener {

private DatePickerDialog mDatePicker;

@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context,int theme, OnDateSetListener callBack,
        int year, int monthOfYear, int dayOfMonth) {
    super(context, theme,callBack, year, monthOfYear, dayOfMonth);
    mDatePicker = new DatePickerDialog(context,theme,callBack, year, monthOfYear, dayOfMonth);

    mDatePicker.getDatePicker().init(2013, 7, 16, this);

    updateTitle(year, monthOfYear);

}
public void onDateChanged(DatePicker view, int year,
        int month, int day) {
    updateTitle(year, month);
}
private void updateTitle(int year, int month) {
    Calendar mCalendar = Calendar.getInstance();
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
//       mCalendar.set(Calendar.DAY_OF_MONTH, day);
        mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));

}   

public DatePickerDialog getPicker(){

    return this.mDatePicker;
}
    /*
     * the format for dialog tile,and you can override this method
     */
public SimpleDateFormat getFormat(){
    return new SimpleDateFormat("MMM, yyyy");
};   
}

以下是将日期及其微调器从日期选择器对话框中隐藏的代码

CustomDatePickerDialog dp = new CustomDatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog,  datePickerListener, year, month, day);

               DatePickerDialog obj = dp.getPicker();
              try{
                         Field[] datePickerDialogFields = obj.getClass().getDeclaredFields();
                         for (Field datePickerDialogField : datePickerDialogFields) { 
                             if (datePickerDialogField.getName().equals("mDatePicker")) {
                                 datePickerDialogField.setAccessible(true);
                                 DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
                                 Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
                                 for (Field datePickerField : datePickerFields) {
                                    if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField
                                      .getName())) {
                                       datePickerField.setAccessible(true);
                                       Object dayPicker = new Object();
                                       dayPicker = datePickerField.get(datePicker);
                                       ((View) dayPicker).setVisibility(View.GONE);
                                    }
                                 }
                              }

                           }
                         }catch(Exception ex){
                         }
              obj.show();
于 2013-08-20T08:54:51.553 回答