HIDE year from date picker widget

2020-05-06 13:21发布

问题:

I am trying to hide the year field from the date picker widget.This may look like a repeated question but the answers given for previous questions are not helping me to hide the year field..

Here goes my code::

@Override 
protected void onCreate(Bundle savedInstanceState) {

    int month = 0;
    int year = 0;
    int day = 0;
    OnDateSetListener datePickerListener = null;
    CustomDatePickerDialog dp = new CustomDatePickerDialog(getBaseContext(), android.R.style.Theme_Holo_Light_Dialog,  datePickerListener, year, month, day);
    DatePickerDialog obj = dp.getPicker();

    picker = (DatePicker) findViewById(R.id.datePicker1);
    try {
          java.lang.reflect.Field[] f = obj.getClass().getDeclaredFields();
  //      Field f[] = picker.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            if (field.getName().equals("prabs")) {
                field.setAccessible(true);
                DatePicker datePicker = (DatePicker) field.get(obj);
                java.lang.reflect.Field datePickerFields[] = obj.getClass().getDeclaredFields();
                for (java.lang.reflect.Field datePickerField : datePickerFields) {
                    if ("mYearPicker".equals(datePickerField.getName()) || "mYearSpinner".equals(datePickerField
                            .getName()))
                    {
                             datePickerField.setAccessible(true);
                             Object yearPicker = new Object();
                             yearPicker = datePickerField.get(datePicker);
                             ((View) yearPicker).setVisibility(View.GONE);
                    }
                }
           /*     Object yearPicker = new Object();
                yearPicker = field.get(picker);
                ((View) yearPicker).setVisibility(View.GONE);*/
            }
        }
    } 
    catch (SecurityException e) {
        Log.d("ERROR", e.getMessage());
    } 
    catch (IllegalArgumentException e) {
        Log.d("ERROR", e.getMessage());
    } 
    catch (IllegalAccessException e) {
        Log.d("ERROR", e.getMessage());
    }
    obj.show();
}

outside the main class::

class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener
{
    private DatePickerDialog prabs;

    @SuppressLint("NewApi")
    public CustomDatePickerDialog(Context context, int theme,
            OnDateSetListener listener, int year, int monthOfYear,
            int dayOfMonth) 
    {
        super(context, theme, listener, year, monthOfYear, dayOfMonth);
        // TODO Auto-generated constructor stub
        prabs = new DatePickerDialog(context, theme, listener, year, monthOfYear, dayOfMonth);
        prabs.getDatePicker().init(2015, 01, 8, this);  
        updateTitle(monthOfYear,dayOfMonth);
    }

    private void updateTitle(int Month, int Day)
    {
        // TODO Auto-generated method stub
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.MONTH, Month);
        mCalendar.set(Calendar.DAY_OF_MONTH, Day);
        prabs.setTitle(getFormat().format(mCalendar.getTime()));
    }

    public SimpleDateFormat  getFormat() {
        // TODO Auto-generated method stub
        return new SimpleDateFormat("MMM, dd");
    }

    public void onDateChanged(DatePicker view,int year, int month,int day)
    {
        updateTitle(month,day);
    }

    public DatePickerDialog getPicker()
    {
        return this.prabs;
    }
}

before adding obj.show(); it was displaying the entire widget with calender,but I want only widget with date and month

trying to solve this from many days. But failed every time :( Any suggestions would be of great help.. Thank you..

EDIT::

year field means the date picker widget's year field of eclipse

I referred How to display year only in date picker in android and Custom Date Picker Dialog

Error log::

01-09 11:26:20.780: E/AndroidRuntime(23840): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application this is a crash report

回答1:

Try this, it works for me.

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        // hide year from datepicker
        DatePickerDialog dlg = new DatePickerDialog(this,
                datePickerListener, year, month, day) {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                int year = getContext().getResources().getIdentifier(
                        "android:id/year", null, null);
                if (year != 0) {
                    View yearPicker = findViewById(year);
                    if (yearPicker != null) {
                        yearPicker.setVisibility(View.GONE);
                    }
                }
            }
        };
        return dlg;
    }
    return null;
}


回答2:

try following code

final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
} 

CustomerDatePickerDialog

class CustomerDatePickerDialog extends DatePickerDialog {
 public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
     super(context, callBack, year, monthOfYear, dayOfMonth);
 }

 @Override
 public void onDateChanged(DatePicker view, int year, int month, int day) {
     super.onDateChanged(view, year, month, day);
     mDialog.setTitle((month + 1) + "-" + day + "-");
 }
}

DAtePicker

   private DatePicker findDatePicker(ViewGroup group) {
    if (group != null) {
        for (int i = 0, j = group.getChildCount(); i < j; i++) {
            View child = group.getChildAt(i);
            if (child instanceof DatePicker) {
                return (DatePicker) child;
            } else if (child instanceof ViewGroup) {
                DatePicker result = findDatePicker((ViewGroup) child);
                if (result != null)
                    return result;
            }
        }
    }
    return null;

}