从DatepickerDialog android隐藏日期和月份

时间:2014-11-08 06:48:13

标签: android datepickerdialog

如何从datepicker对话框中隐藏特定的日期和月份字段以及日期和年份字段以及日期和月份和年份字段?

2 个答案:

答案 0 :(得分:0)

我担心没有简单的方法可以做到这一点。您需要创建自己的自定义类。

在这里看一下接受的答案:

Hide Date from Date Picker

答案 1 :(得分:0)

尝试这样,

为日期选择器创建自定义类是以下每次用户更改月/年时将更新标题中的日期

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