繁体   English   中英

如何使用十六进制值更改 CheckedTextView 的选中色调颜色

[英]How to Change The Checked Tint Color of a CheckedTextView with Hex Value

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

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

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

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的颜色创建一个颜色列表:

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设置了什么状态? 有没有更简单的方法来做到这一点? 是我想多了?

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

android:drawableTint="@color/grey_text"

它看起来像在CheckedTextView着色是非常错误的。 最后,我通过交换onClickListener颜色来解决它:

checkedTextView.setOnClickListener {
    if (checkedTextView.isChecked) {
        checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1)
    } else {
        checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2)
    }
}

(例如在Kotlin,Java类似)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM