更改NumberPicker的文本颜色 [英] Change the text color of NumberPicker

查看:333
本文介绍了更改NumberPicker的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过最全的线程就这个问题和没有提供问题的解答。样式化NumberPicker不工作(按照本主题: NumberPicker textColour

I've looked at most all of the threads on this and none provided an answer that works. Styling the NumberPicker does not work (as per this thread: NumberPicker textColour)

在numberPicker的样式属性设置为具有颜色项没有任何效果无论是风格。也没有设置文字颜色属性的numberPicker XML做任何事情。

Setting the style attribute on the numberPicker to a style that has a color item does not have any effect either. Nor does setting the textColor attribute on the numberPicker XML do anything.

最近我得这是使用numberPicker施展它的getChildAt(),以一个EditText,然后做setColor()上的EditText,但在初始化时仅每一次改变孩子的颜色一次,然后它从上面的选择;不是我找的任何东西。

Closest I've got to this is using the numberPicker to cast its getChildAt() to an EditText and then do setColor() on that EditText, but that only changes the color of the child once upon initialization and then every time it is selected from thereon; not what I am looking for either.

任何帮助吗?谢谢

推荐答案

这code应该解决您的问题。所遇到的问题是,因为NumberPicker施工时它捕获的EditText文字颜色和分配给到油漆,因此它可以绘制的上方和下方具有相同的颜色的编辑文本的数字。

This code should solve your problem. The problem you are experiencing is because during the construction of NumberPicker it captures the EditText textColor and assigns to to a paint so it can draw the numbers above and below the edit text with the same color.

import java.lang.reflect.Field;

public static boolean setNumberPickerTextColor(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);
                ((EditText)child).setTextColor(color);
                numberPicker.invalidate();
                return true;
            }
            catch(NoSuchFieldException e){
                Log.w("setNumberPickerTextColor", e);
            }
            catch(IllegalAccessException e){
                Log.w("setNumberPickerTextColor", e);
            }
            catch(IllegalArgumentException e){
                Log.w("setNumberPickerTextColor", e);
            }
        }
    }
    return false;
}

这篇关于更改NumberPicker的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆