zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.basic.security.utils;
 
import android.app.Activity;
import android.widget.DatePicker;
import android.widget.NumberPicker;
 
import java.lang.reflect.Field;
 
 
public class DatePickerUtil {
 
    public static void setFont(Activity activity, DatePicker datepicker) {
        if (android.os.Build.VERSION.SDK_INT >= 20)
            initLollipop(activity, datepicker);
        else if (android.os.Build.VERSION.SDK_INT >= 14)
            initKitkat(activity, datepicker);
    }
 
    private static void initLollipop(Activity activity, DatePicker datepicker) {
        try {
 
            Field delegateField = datepicker.getClass().getDeclaredField(
                    "mDelegate");
            delegateField.setAccessible(true);
            Object delegate = new Object();
            delegate = delegateField.get(datepicker);
 
            Field field = delegate.getClass().getDeclaredField("mDaySpinner");
            setFont(activity, delegate, field);
            field = delegate.getClass().getDeclaredField("mMonthSpinner");
            setFont(activity, delegate, field);
            field = delegate.getClass().getDeclaredField("mYearSpinner");
            setFont(activity, delegate, field);
            setFontEditText(activity, delegate, delegate.getClass()
                    .getDeclaredField("mDaySpinnerInput"));
            setFontEditText(activity, delegate, delegate.getClass()
                    .getDeclaredField("mMonthSpinnerInput"));
            setFontEditText(activity, delegate, delegate.getClass()
                    .getDeclaredField("mYearSpinnerInput"));
 
        } catch (SecurityException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (Exception e) {
        }
    }
 
    private static void initKitkat(Activity activity, DatePicker datepicker) {
        try {
            Field field = datepicker.getClass().getDeclaredField("mDaySpinner");
            setFont(activity, datepicker, field);
            field = datepicker.getClass().getDeclaredField("mMonthSpinner");
            setFont(activity, datepicker, field);
            field = datepicker.getClass().getDeclaredField("mYearSpinner");
            setFont(activity, datepicker, field);
            setFontEditText(activity, datepicker, datepicker.getClass()
                    .getDeclaredField("mDaySpinnerInput"));
            setFontEditText(activity, datepicker, datepicker.getClass()
                    .getDeclaredField("mMonthSpinnerInput"));
            setFontEditText(activity, datepicker, datepicker.getClass()
                    .getDeclaredField("mYearSpinnerInput"));
 
        } catch (SecurityException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (Exception e) {
        }
    }
 
    private static void setFont(Activity activity, Object datepicker,
                                Field field) throws Exception {
        field.setAccessible(true);
        Object yearPicker = new Object();
        yearPicker = field.get(datepicker);
        if (yearPicker instanceof NumberPicker) {
            NumberPicker npYear = (NumberPicker) yearPicker;
        }
 
 
//        ((View) yearPicker).setVisibility(View.VISIBLE);
//        //((View) yearPicker).setPadding(-2, 0, -2, 0);
//
//        View childpicker;
//        childpicker = (View) activity.findViewById(
//                Resources.getSystem()
//              .getIdentifier("month", "id", "android"));
        Field field1 = yearPicker.getClass().getDeclaredField("mInputText");
        field1.setAccessible(true);
        Object edittext = new Object();
        edittext = field1.get(yearPicker);
 
        Field field2 = yearPicker.getClass().getDeclaredField(
                "mSelectorWheelPaint");
        field2.setAccessible(true);
        Object paint = new Object();
        paint = field2.get(yearPicker);
//        if (CustomFontTextview.sTypeface == null)
//            CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
//        ((Paint) paint).setTypeface(Typeface.create(CustomFontTextview.sTypeface, Typeface.BOLD));
        //((Paint) paint).setTextSize(MainActivity);
//        ((Paint) paint).setColor(Color.RED);
 
        //((TextView) edittext).setTypeface(CustomFontTextview.sTypeface, Typeface.BOLD);
//        ((TextView) edittext).setTextColor(Color.RED);
        //((TextView) edittext).setTextSize(BaseApplication.getApplication().activity.getResources().getDimension(R.dimen.w14dp));
    }
 
    private static void setFontEditText(Activity activity, Object datePicker,
                                        Field field) throws Exception {
        field.setAccessible(true);
        Object paint = new Object();
        paint = field.get(datePicker);
//        if (CustomFontTextview.sTypeface == null)
//            CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
//        ((EditText) paint).setTypeface(CustomFontTextview.sTypeface,Typeface.BOLD);
//        ((EditText) paint).setTextColor(Color.RED);
        //((EditText) paint).setTextSize(BaseApplication.getApplication().activity.getResources().getDimension(R.dimen.w14dp));
    }
 
}