1

我想CheckedTextView在视图状态为时动态更改 a 的色调颜色checked。我很确定我可以通过调用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设置了什么?有没有更简单的方法来做到这一点?我是不是想多了?

4

2 回答 2

1

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

android:drawableTint="@color/grey_text"

于 2021-08-14T11:44:00.693 回答
0

看起来在 aCheckedTextView中着色非常有问题。最后我通过交换颜色解决了它onClickListener

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

(Kotlin 中的示例,Java 类似)

于 2016-10-23T16:36:13.153 回答