[Android] NumberPicker选择的项目更改颜色 [英] [Android]NumberPicker selected item change color

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

问题描述

是否可以在数字选择器中更改所选项目的颜色,以便每次出现新的中心子TextView时,将其颜色更改为我喜欢的颜色,而我没有发现任何与此相关的样式或API?

Is it possible to change color of the selected item in numberpicker so each time a new center child TextView appear change its color to whatever I like I did not find any style or API expose about this ?

推荐答案

我尝试遵循许多答案,所有答案都在设置整数的颜色.

I tried to follow many answers and all of them were setting the whole number's color.

我想出了如何仅更改所选编号的颜色 r.在您的NumberPicker中设置OnTouchListener.

I figured out how to change color only for selected number. set OnTouchListener in your NumberPicker.

numberPicker.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(final View v, MotionEvent event) {
                        currentTouchAction = event.getAction();
                        if (currentTouchAction == MotionEvent.ACTION_UP) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (currentTouchAction == MotionEvent.ACTION_UP) {
                                        setSelectedTextColor((NumberPicker)v, selectedColorRes);
                                    }
                                }
                            }, 300);
                        }
                        return false;
                    }
                });

及以下代码用于更改所选数字的颜色.您可能必须使用"performClick()"动态地应用颜色.

and below code is for changing the selected number color. You might have to use 'performClick()' to dynamically apply the color.

如果您这样做,选择器将立即停止.这就是为什么我将postDelayed(300)放在上面.用户可以多次拖动数字选择器以选择利润数字.如果将其放在postDelay()上方并等待几毫秒,则可以平滑滚动.

And if you do that, the picker would immediately stop. So that's why I put postDelayed(300) above. User might drag number picker several times to select the profit number. If you put above postDelay() and wait for several milliseconds, it can be scrolled smoothly.

public void setSelectedTextColor(NumberPicker np, int colorRes) {
    final int count = np.getChildCount();
    for(int i = 0; i < count; i++){
        View child = np.getChildAt(i);
        if(child instanceof EditText){
            try{
                Field selectorWheelPaintField = np.getClass().getDeclaredField("mSelectorWheelPaint");
                selectorWheelPaintField.setAccessible(true);
                ((EditText) child).setTextColor(mContext.getResources().getColor(colorRes));
                np.performClick();
            }
            catch(NoSuchFieldException e){
            }
            catch(IllegalArgumentException e){
            }
        }
    }
}

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

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