HIDE year from date picker widget

后端 未结 2 381
南方客
南方客 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: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;
    }
    

提交回复
热议问题