如何更改Android中的数字选择器中心文本颜色

我已经实现了一个自定义日期选择器,其中我需要为其中的数字选择器(日期选择器使用数字选择器)提供自定义颜色,如下所示:

this answer的帮助下,我可以看起来像这样:

但正如您所看到的那样,实际日期会以黄色而不是白色显示.
而且,我尝试在运行时这样做:

npMonth.setOnValueChangedListener(new OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker,int oldVal,int newVal) {
                EditText et = ((EditText) npMonth.getChildAt(0));
                et.setTextColor(getResources().getColor(R.color.gold));
            }
        });

但仍然没有运气.

如何将中心文本颜色更改为白色并将顶部和底部文本保持为黄色?

解决方法

最简单的方法
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);
            }
        }
    }
}

相关文章

文章浏览阅读8.8k次,点赞9次,收藏20次。本文操作环境:win1...
文章浏览阅读1.2w次,点赞15次,收藏69次。实现目的:由main...
文章浏览阅读3.8w次。前言:最近在找Android上的全局代理软件...
文章浏览阅读2.5w次,点赞17次,收藏6次。创建项目后,运行项...
文章浏览阅读8.9w次,点赞4次,收藏43次。前言:在Android上...
文章浏览阅读1.1w次,点赞4次,收藏17次。Android Studio提供...