我在 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);
}
}
});
问题是它没有改变正确的东西。关于如何更改此颜色的任何想法?
最佳答案
要为 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/