HIDE year from date picker widget

后端 未结 2 383
南方客
南方客 2021-01-28 00:41

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

相关标签:
2条回答
  • 2021-01-28 01:25

    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;
    
    } 
    
    0 讨论(0)
  • 2021-01-28 01:33

    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;
    }
    
    0 讨论(0)
提交回复
热议问题