利用反射来更改Android原生DatePicker文字与分割线颜色

来源:互联网 发布:盟军敢死队2 mac版 编辑:程序博客网 时间:2024/06/05 11:42

啥也不说了,上代码!

private void setDatePickerDividerAndTextColor(DatePicker datePicker) {        LinearLayout llFirst = (LinearLayout) datePicker.getChildAt(0);        LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0);        for (int i = 0; i < mSpinners.getChildCount(); i++) {            NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i);            Field[] pickerFields = NumberPicker.class.getDeclaredFields();            for (Field pf : pickerFields) {                if (pf.getName().equals("mSelectorWheelPaint")) {                    pf.setAccessible(true);                    Paint paint = new Paint();                    paint.setTextSize(Utils.sp2px(20, getApplicationContext()));                    paint.setTextAlign(Align.CENTER);                    paint.setColor(getResources().getColor(R.color.white));                    try {                        pf.set(picker, paint);                    } catch (IllegalArgumentException e) {                        e.printStackTrace();                    } catch (NotFoundException e) {                        e.printStackTrace();                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    }                    break;                }            }            for (Field pf : pickerFields) {                if (pf.getName().equals("mSelectionDivider")) {                    pf.setAccessible(true);                    try {                        pf.set(picker, new ColorDrawable(this.getResources().getColor(R.color.white)));                    } catch (IllegalArgumentException e) {                        e.printStackTrace();                    } catch (NotFoundException e) {                        e.printStackTrace();                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    }                    break;                }            }        }    }

原理很简单,首先是通过反射遍历DatePicker中的所有Field,

内部的两个For循环中第一个是更改文字颜色

更改文字的颜色是通过反射获取NumberPicker的Paint,利用自己定义的paint来为其赋值

同理也可以更改字号,

第二个For循环则是为了变更分割线的颜色,分割线是Drawable类型,所以对其直接修改

上面的这个方法还有一点遗漏,就是当前选中的字颜色并未更改,所以还需要以下这个方法

private void changeDataPickerTextColor() {        try {            Field f[] = mBirth.getClass().getDeclaredFields();            for (Field field : f) {                if (field.getName().equals("mYearSpinner")) {                    field.setAccessible(true);                    Object yearPicker = new Object();                    yearPicker = field.get(mBirth);                    View childpicker;                    childpicker = (View) findViewById(Resources.getSystem().getIdentifier("year", "id", "android"));                    EditText textview = (EditText) childpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));                    // textview.setTextSize(26);                    // textview.setPadding(-3, 0, -3, 0);                    textview.setTextColor(getResources().getColor(R.color.white));                    // System.out.println("ss1:"+field);                }                if (field.getName().equals("mDaySpinner")) {                    field.setAccessible(true);                    Object yearPicker = new Object();                    yearPicker = field.get(mBirth);                    ((View) yearPicker).setVisibility(View.VISIBLE);                    ((View) yearPicker).setPadding(-2, 0, -2, 0);                    View childpicker;                    childpicker = (View) findViewById(Resources.getSystem().getIdentifier("month", "id", "android"));                    EditText textview = (EditText) childpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));                    // textview.setTextSize(26);                    // textview.setPadding(-3, 0, -3, 0);                    textview.setTextColor(getResources().getColor(R.color.white));                }                if (field.getName().equals("mMonthSpinner")) {                    field.setAccessible(true);                    Object yearPicker = new Object();                    yearPicker = field.get(mBirth);                    // ((View) yearPicker).setVisibility(View.VISIBLE);                    // ((View) yearPicker).setPadding(-2, 0, -2, 0);                    //                    View childpicker;                    childpicker = (View) findViewById(Resources.getSystem().getIdentifier("day", "id", "android"));                    EditText textview = (EditText) childpicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));                    // textview.setTextSize(26);                    // textview.setPadding(-3, 0, -3, 0);                    textview.setTextColor(getResources().getColor(R.color.white));                }            }        } catch (SecurityException e) {            Log.d("ERROR", e.getMessage());        } catch (IllegalArgumentException e) {            Log.d("ERROR", e.getMessage());        } catch (IllegalAccessException e) {            Log.d("ERROR", e.getMessage());        } catch (Exception e) {            Log.d("ERROR", e.getMessage());        }    }

第一篇文章,我自己都佩服我自己

*文章内如果有遗漏或问题欢迎指正,谢谢。

0 0