Java 如何以编程方式更改复选框选中的颜色
我正在Android中使用复选框视图。我想在它被选中时改变它的颜色。现在,当它被选中时,它是默认的深绿色,我想把它改成不同的颜色,当它没有被选中时,只是默认的颜色 这是我的密码:Java 如何以编程方式更改复选框选中的颜色,java,android,checkbox,colors,Java,Android,Checkbox,Colors,我正在Android中使用复选框视图。我想在它被选中时改变它的颜色。现在,当它被选中时,它是默认的深绿色,我想把它改成不同的颜色,当它没有被选中时,只是默认的颜色 这是我的密码: CheckBox c = new CheckBox(this); c.setId(View.generateViewId()); c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
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);
}
}
});
问题是它并没有改变正确的事情。关于如何改变这种颜色有什么想法吗
您是否尝试创建一个
选择器
并将该选择器
分配给您的复选框
,例如:
//drawable file called cb_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/checked" />
<item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>
在res中实现此文件
然后将按钮添加到复选框
用AppCompatCheckBox
替换您的复选框
,并调用以下方法:
public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
checkBox.setSupportButtonTintList(colorStateList);
}
要对CompoundButton着色,请尝试对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
}
if(Build.VERSION.SDK\u INT<21){
CompoundButtonCompat.SetButtonContIntList(按钮,ColorStateList.valueOf(tintColor));//必要时使用android.support.v4.widget.CompoundButtonCompat
}否则{
button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList可直接在API>19上访问
}
此解决方案不适用于API级别17(可能还有API 21以下的任何内容)。最后一行应该是“CompoundButtonCompat.setButtonCintList(复选框,colorStateList)”,如果不起作用,请使用ContextCompat.getColor(checkBox.getContext,uncheckedColor)包装取消选中的颜色,第一个分支本身就足够了。根本不需要检查SDK版本。
implement this file in res
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_on_background_focus_yellow" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_off_background_focus_yellow" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_on_background" />
</selector>
and then add button to checkbox
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox" />
public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
checkBox.setSupportButtonTintList(colorStateList);
}
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
}