如何在android中更改数字选择器的所选选项的颜色

时间:2015-07-21 10:47:15

标签: android

public static void setNumberPickerTextColor(Context context,NumberPicker numberPicker, int color){
        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);
                    numberPicker.invalidate();
                    ((EditText)child).setTextColor(color);

                }catch(Exception e){
                   e.printStackTrace();
                } 

            } 
        } 
    }
  

以上代码更改所有选项的颜色。但我想改变   仅选择了选项颜色

提前致谢!!!!

1 个答案:

答案 0 :(得分:0)

您在所有选项上使用for循环来更改所有选项的颜色。

您只想更改所选项目的颜色,因此您只能选择所选的子项,而不是for循环:

View child = numberPicker.getChildAt(numberPicker.getValue());
//change the color of this child to the new color