如何在android中更改数字选择器选定选项的颜色 public static void setNumberPickerTextColor(上下文上下文,NumberPicker NumberPicker,int-color){ final int count=numberPicker.getChildCount(); for(int i=0;i

如何在android中更改数字选择器选定选项的颜色 public static void setNumberPickerTextColor(上下文上下文,NumberPicker NumberPicker,int-color){ final int count=numberPicker.getChildCount(); for(int i=0;i,android,Android,以上代码更改了所有选项的颜色。但我想改变 仅选择选项颜色 提前感谢您正在对所有选项使用for循环来更改所有选项的颜色 您只想更改选定项目的颜色,因此,您可以只获取选定的子项,而不是for循环: public static void setNumberPickerTextColor(Context context,NumberPicker numberPicker, int color){ final int count = numberPicker.getChildCount()

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


提前感谢

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

您只想更改选定项目的颜色,因此,您可以只获取选定的子项,而不是for循环:

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();
                } 

            } 
        } 
    }

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

您只想更改选定项目的颜色,因此,您可以只获取选定的子项,而不是for循环:

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();
                } 

            } 
        } 
    }

请查看这是我的确切要求。我不确定numberPickers的行为是否类似于微调器,其中所选的子项是0处的子项。您可以尝试使用getChildAt(newVal)而不是getChildAt(0)提供的链接代码(OnValueChange函数的newVal和oldVal参数提供所选项目的位置以及之前所选项目的位置)请查看这是我的确切要求。我不确定numberPickers的行为是否类似于微调器,其中所选的子项是0处的子项。您可以尝试使用getChildAt(newVal)而不是getChildAt(0)提供的链接代码(OnValueChange函数的newVal和oldVal参数提供所选项目的位置以及之前所选项目的位置)