Android Date selector with only Day and Month

前端 未结 2 860
难免孤独
难免孤独 2021-02-15 17:37

I am looking for a way to customize the Date selector in Android to only show the day and month (i.e. no year).

I am creating a dialog based on How to display date picke

2条回答
  •  难免孤独
    2021-02-15 17:48

    Here give this a go. Its APIv11+ but there are other ways on lower API versions.

    DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
        dueDateCalendar.get(Calendar.YEAR), 
        dueDateCalendar.get(Calendar.MONTH), 
        dueDateCalendar.get(Calendar.DAY_OF_MONTH));
    int year = context.getResources().getIdentifier("android:id/year", null, null);
    if(year != 0){
        View yearPicker = dlg.getDatePicker().findViewById(year);
        if(yearPicker != null){
            yearPicker.setVisibility(View.GONE);
        }
    }
    return dlg;
    

    Updated code: This should do the job.

    DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
        dueDateCalendar.get(Calendar.YEAR), 
        dueDateCalendar.get(Calendar.MONTH), 
        dueDateCalendar.get(Calendar.DAY_OF_MONTH))
    {
        @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;
    

提交回复
热议问题