Personalized control of DatePicker

Focus on personal work acrobatics, personal work QQ: 2686696882

PS: make partial control over the time control of DatePicker and record it for quick use in the future.

First of all, let's talk about the use of DatePicker: (we all know about it, just paste it with code, no longer explain it)

                   Calendar c = Calendar.getInstance();
                        String s = date_et.getText().toString();
                        int month = !s.contains("-") ? c.get(Calendar.MONTH) : Integer.parseInt(StringUtils.split(s, '-')[1]) - 1;
                        new DatePickerDialog(mContext, 0, new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear,
                                                  int startDayOfMonth) {
                                int month = startMonthOfYear + 1;
//                                String m = "";
//                                if (month < 10) {
//                                    m = "0" + month;
//                                } else {
//                                    m = month + "";
//                                }
                                date_et.setText(String.format("%d-%s", startYear, month));
                            }
                        }, c.get(Calendar.YEAR), month, c.get(Calendar.DATE), false).show();

The above part is the code used, no special instructions will be given. (you can deduct me if you need)

The following are the requirements for control personality:

1. Hide the single day of the year, such as the hidden day, only the year and year are displayed

This part is mainly to get the control properties and control them. There are differences before and after version 5.0

private void hideDay(DatePicker mDatePicker) {
    try {
        /* Dealing with special situations above Android 5.0 */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                View daySpinner = mDatePicker.findViewById(daySpinnerId);
                if (daySpinner != null) {
                    daySpinner.setVisibility(View.GONE);
                }
            }
        } else {
            Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
            for (Field datePickerField : datePickerfFields) {
                if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
                    datePickerField.setAccessible(true);
                    Object dayPicker = new Object();
                    try {
                        dayPicker = datePickerField.get(mDatePicker);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                    ((View) dayPicker).setVisibility(View.GONE);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2. Do not allow the control to be edited, such as non editable year (the code is as follows, and the principle is the same as above)

private void enableYear(DatePicker mDatePicker) {
    try {
        /* Dealing with special situations above Android 5.0 */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) {
                View yearSpinner = mDatePicker.findViewById(yearSpinnerId);
                if (yearSpinner != null) {
                    yearSpinner.setEnabled(false);
                }
            }
        } else {
            Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
            for (Field datePickerField : datePickerfFields) {
                if ("mYearSpinner".equals(datePickerField.getName()) || ("mYearPicker").equals(datePickerField.getName())) {
                    datePickerField.setAccessible(true);
                    Object yearPicker = new Object();
                    try {
                        yearPicker = datePickerField.get(mDatePicker);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                    ((View) yearPicker).setEnabled(false);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The above DatePickerDialog custom class, I will upload a file in my resources, you can download it if you need, and give me a small point (too much) thank you

https://download.csdn.net/download/mr_woniu/10665102

Keywords: Android

Added by larus@spuni.is on Thu, 02 Jan 2020 15:04:22 +0200