Android - DatePickerDialog 標題與設定年份最大最小值、年份格式、線條顏色變更

Android - DatePickerDialog 標題與設定年份最大最小值

Android - DatePickerDialog 標題與設定年份最大最小值

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getDatePicker().setMinDate(minDate);
dialog.getDatePicker().setMaxDate(maxDate);

隱藏線條與變更為民國年

public void changeDividerColor() {
    try {
        Class internalRID = Class.forName("com.android.internal.R$id");
        Field month = internalRID.getField("month");
        Field day = internalRID.getField("day");
        Field year = internalRID.getField("year");
        NumberPicker npMonth = (NumberPicker) findViewById(month.getInt(null));
        NumberPicker npDay = (NumberPicker) findViewById(day.getInt(null));
        NumberPicker npYear = (NumberPicker) findViewById(year.getInt(null));
  
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            npYear.setFormatter(new NumberPicker.Formatter() {
                @Override
                public String format(int value) {
                    int chinese_year = value-1911;
                    return "民國"+String.valueOf(chinese_year)+"年";
                }
            });
        }
        setDividerColor(npMonth);
        setDividerColor(npDay);
        setDividerColor(npYear);
    } catch (Exception ignored) {
    }
}
private void setDividerColor(NumberPicker numberPicker) {
    Field[] numberPickerFields = NumberPicker.class.getDeclaredFields();
    for (Field field : numberPickerFields) {
        if (field.getName().equals("mSelectionDivider")) {
            field.setAccessible(true);
            try {
                field.set(numberPicker, ContextCompat.getDrawable(getContext(),
                        R.drawable.bg_datepicker_divider));
            } catch (Exception ignored) {
            }
            break;
        }
    }
}