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

时间:2016-04-07 16:25:23

标签: android colors checkedtextview statelist

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

所以,我可以获得ColorStateList的{​​{1}},但我不知道如何更改 CheckedTextView州的颜色。我知道我可以创建一个新的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}设置的状态呢?有更简单的方法吗?我是否在思考它?

1 个答案:

答案 0 :(得分:0)

看起来像CheckedTextView中的着色很漂亮。最后,我通过交换onClickListener中的颜色来解决它:

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

(Kotlin中的例子,Java类似)

相关问题