【问题标题】:How change color of selected option of number picker in android如何在android中更改数字选择器的选定选项的颜色
【发布时间】:2015-07-21 10:47:15
【问题描述】:
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();
                } 

            } 
        } 
    }

上面的代码改变了所有选项的颜色。但我想改变 只选择选项颜色

提前致谢!!!!

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

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

    【讨论】:

    • 请查看stackoverflow.com/questions/26887425/…这是我的确切要求。
    • 我不确定 numberPickers 是否表现得像微调器,其中选定的孩子是 0 的孩子。您可以尝试使用 getChildAt(newVal) 而不是 getChildAt(0) 提供的链接代码( OnValueChange 函数的 newVal 和 oldVal 参数给出了选中项的位置和之前选中项的位置)
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2012-06-22
    相关资源
    最近更新 更多