【问题标题】:How to change color for selected number in enabled NumberPicker in Android如何在 Android 中启用 NumberPicker 中更改所选数字的颜色
【发布时间】:2016-11-07 18:21:55
【问题描述】:

我想在 MyNumberPicker 上实现这个效果

到目前为止,我实现了一个 NumberPicker 类并更改了几个属性,但有趣的部分在这里:

public class NumberPicker extends android.widget.NumberPicker{

    public NumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        updateView(child);
    }

    @Override
    public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        updateView(child);
    }

    private void updateView(View view) {
        if(view instanceof EditText){
            ((EditText) view).setTextSize(getResources().getDimension(R.dimen.numberpicker_text_size));
           ((EditText) view).setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));

        }
    }

但是我得到的效果是这样的:

我得到所有蓝色文本(选中、未选中、活动、非活动)如何仅在启用 NumberPicker 和选定值时获得此蓝色?

感谢支持

【问题讨论】:

  • stackoverflow.com/a/56677573/10898426 中有答案。您可以使用 setOnTouchListener 并更改 numberPicker 的 EditText 的颜色以更改唯一选定的数字,就像您问题的第一张图片一样。

标签: android numberpicker


【解决方案1】:

使用您想要的属性创建一个ColorStateList。然后将这些应用到EditText

final ColorStateList colors = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_selected},
                        new int[]{-android.R.attr.state_selected}
                },
                new int[]{selectedColorCode, defaultColor}
        );

((EditText) view).setTextColor(colors);

【讨论】:

  • 我使用了 selectedColorCode 和 defaultColor colorPrimary 和 colorPrimaryDark,但是我只看到 colorPrimary 应用了,那么这和 ((EditText) view).setTextColor(ContextCompat.getColor(getContext(), R .color.colorPrimary));我只是在 colorPrimary 中看到它们
猜你喜欢
  • 2016-03-24
  • 2016-03-06
  • 2013-09-19
  • 1970-01-01
  • 2014-05-22
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多