首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改带有十六进制值的CheckedTextView的选中Tint颜色

如何更改带有十六进制值的CheckedTextView的选中Tint颜色
EN

Stack Overflow用户
提问于 2016-04-07 16:25:23
回答 3查看 2.4K关注 0票数 2

当视图的状态为CheckedTextView时,我想动态地更改checked的色调。我确信我可以通过在setCheckMarkTintList上调用CheckedTextView来实现这一点。要做到这一点,我需要一个ColorStateList,但问题是,我希望保留CheckedTextView的每个状态的所有颜色,除了checked状态。

因此,我可以获得ColorStateListCheckedTextView,但我不知道一种方法,只改变颜色的checked状态。我知道我可以创建一个新的ColorStateList,但是如何确保它保留原始的所有值?

我可以创建这样的状态列表:

代码语言:javascript
复制
int[][] states = new int[][] {
    new int[]{android.R.attr.state_pressed},
    new int[]{-android.R.attr.state_pressed},
    new int[]{android.R.attr.state_focused},
    new int[]{-android.R.attr.state_focused},
    new int[]{android.R.attr.state_selected},
    new int[]{-android.R.attr.state_selected},
    new int[]{android.R.attr.state_checkable},
    new int[]{-android.R.attr.state_checkable},
    new int[]{android.R.attr.state_checked},
    new int[]{-android.R.attr.state_checked},
    new int[]{android.R.attr.state_enabled},
    new int[]{-android.R.attr.state_enabled},
    new int[]{android.R.attr.state_window_focused},
    new int[]{-android.R.attr.state_window_focused},
    new int[]{} // default state
}

并根据原始ColorStateList中的颜色创建颜色列表。

代码语言:javascript
复制
int[] colors = new int[] {
    stateList.getColorForState(new int[]{android.R.attr.state_pressed}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_pressed}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{android.R.attr.state_focused}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_focused}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{android.R.attr.state_selected}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_selected}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{android.R.attr.state_checkable}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_checkable}, stateList.getDefaultColor()),
    Color.parseColor(colorHexValue),
    stateList.getColorForState(new int[]{-android.R.attr.state_checked}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{android.R.attr.state_enabled}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_enabled}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{android.R.attr.state_window_focused}, stateList.getDefaultColor()),
    stateList.getColorForState(new int[]{-android.R.attr.state_window_focused}, stateList.getDefaultColor()),
    stateList.getDefaultColor()
}

但这只会覆盖孤独的州..。您还可以组合状态,例如new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed, -android.R.attr.state_checked}。试图解释每一种可能的状态是很荒谬的,那么我怎么可能知道原始ColorStateList设置了哪些状态呢?有更简单的方法吗?我是不是想得太多了?

EN

回答 3

Stack Overflow用户

发布于 2021-08-14 11:44:00

这会将CheckedTextView颜色从绿色更改为指定的任何颜色。

Android:drawableTint=“@color/灰色文本”

票数 1
EN

Stack Overflow用户

发布于 2016-10-23 16:36:13

它看起来像在一个CheckedTextView的着色是相当的小马车。最后,我通过在onClickListener中交换颜色来解决这个问题

代码语言:javascript
复制
checkedTextView.setOnClickListener {
    if (checkedTextView.isChecked) {
        checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1)
    } else {
        checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2)
    }
}

(例如在Kotlin,Java类似)

票数 0
EN

Stack Overflow用户

发布于 2022-10-06 07:46:30

如果需要以编程方式更改它,可以设置如下所示的ColorStateList:

代码语言:javascript
复制
                int[] colors = new int[] {
                        color,
                        color,
                        color,
                        color
                };

                int[][] states = new int[][] {
                        new int[] { android.R.attr.state_enabled}, // enabled
                        new int[] {-android.R.attr.state_enabled}, // disabled
                        new int[] {-android.R.attr.state_checked}, // unchecked
                        new int[] { android.R.attr.state_pressed}  // pressed
                };

                ColorStateList myList = new ColorStateList(states, colors);
                checkedTextView.setCheckMarkTintList(myList);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36482003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档