How to change Number Picker center text color in Android

巧了我就是萌 提交于 2019-12-05 05:47:06

The easiest way to do this

setNumberPickerTextColor(mNumberPicker);

public void setNumberPickerTextColor(NumberPicker numberPicker){
    int color=Color.parseColor("#333333");

    final int count = numberPicker.getChildCount();
    for(int i = 0; i < count; i++){
        View child = numberPicker.getChildAt(i);
        if(child instanceof EditText){
            try{
                Field selectorWheelPaintField = numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
                selectorWheelPaintField.setAccessible(true);
                ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
                ((EditText)child).setTextColor(color);
                numberPicker.invalidate();
            }
            catch(NoSuchFieldException e){
                Log.e("setNumberPickerColor1", ""+e);
            }
            catch(IllegalAccessException e){
                Log.e("setNumberPickerColor2", ""+e);
            }
            catch(IllegalArgumentException e){
                Log.e("setNumberPickerColor3", ""+e);
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!