从DatePickerDialog删除年DatePickerDialog

2023-09-13 01:38:35 作者:花悸、莫浅忆

我使用的基本DatePickerDialog教程由机器人在这里找到 Android的日期选择器

I'm using the basic DatePickerDialog tutorial by Android found here Android Date Picker

和我有它的工作。但我只希望能够选择一个日期从今年。那么,有没有办法在今年从对话框中删除,以便只月份和日期的显示?每次我尝试,并采取年度int的列它打破了code。

and I've got it working. But I only want to be able to pick a date from this year. So is there a way to remove the year from the Dialog so that only the month and day are displayed? Every time I try and take the Year int out it breaks the code.

推荐答案

是的,它有可能。 DatePicker的具有内置三NumberPicker控制,其次是年,月,日,躲了第一个。

Yes it possible. DatePicker has the built-in three NumberPicker control, followed by the year, month, day, hide out first to.

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;

    }