java - 如何以编程方式更改复选框选中的颜色

标签 java android checkbox colors

我在 Android 中使用 CheckBox View 。我想在选中时更改它的颜色。现在它是选中时的默认深绿色,我想将其更改为不同的颜色,当未选中时,只是默认颜色。

这是我的代码:

CheckBox c = new CheckBox(this);
c.setId(View.generateViewId());

c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.isChecked())
        {
            buttonView.setBackgroundColor(Color.rgb(64, 131, 207));
        }
        if(!buttonView.isChecked())
        {
            buttonView.setBackgroundColor(Color.WHITE);
        }

    }
});

问题是它没有改变正确的东西。关于如何更改此颜色的任何想法?

enter image description here

最佳答案

要为 CompoundButton Tints 着色,请尝试这个,适用于 API>21 及以下

if (Build.VERSION.SDK_INT < 21) {
    CompoundButtonCompat.setButtonTintList(button, ColorStateList.valueOf(tintColor));//Use android.support.v4.widget.CompoundButtonCompat when necessary else
} else {
    button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList is accessible directly on API>19
}

关于java - 如何以编程方式更改复选框选中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340256/

相关文章:

android - 房间数据库迁移 fallbackToDestructiveMigration() 不工作

android - 自定义键盘重叠 EditText

java - 在偏好 Activity 中静音

html - 清除/覆盖输入元素的 css

html - 在没有标签标签的情况下设置复选框样式

java - LibGdx:表格显示不正确

java - 无法暂停我的线程!为什么?

java - 在 netbeans 中找不到符号

java - Python 解析 Java Double.toHexString(double) 的输出

java - 我需要创建一个可以在所有智能手机上运行的智能手机应用程序,我该怎么做?