[英]How can I change Checkbox color when I click and unclick?
When I click on checkbox color change of that checkbox and rest of check box remain another color so how can I change the color of checkbox?当我单击该复选框的复选框颜色更改并且复选框的 rest 保持另一种颜色时,如何更改复选框的颜色?
if (CountryList.get(pos).getSelected()) {
CountryList.get(pos).setSelected(false);
holder.country.setBackgroundResource(R.color.btnbckgrd);
} else {
CountryList.get(pos).setSelected(true);
holder.country.setBackgroundResource(R.color.lightgreen);
}
try this:
mSelectedItem = -1;
holder.country.setBackgroundResource(mSelectedItem == position ?
R.color.lightgreen: R.color.btnbckgrd);
you can use style for this您可以为此使用样式
<style name="yourStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">your_color</item> <!-- for uncheck state -->
<item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>
set it in xml like this像这样在 xml 中设置它
<CheckBox
android:theme="@style/yourStyle"
/>
Have you tried android:tint="@color/your_color"
in your layout file??您是否在布局文件中尝试过
android:tint="@color/your_color"
?
I think this might help you.我想这可能会对你有所帮助。
if(checkbox.isChecked()){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { checkbox.setButtonTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.yourcheckcolor))); } }else{ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { checkbox.setButtonTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.youruncheckcolor))); } }
Correct answer which is working on all API would be:适用于所有 API 的正确答案是:
Create style:创建风格:
<style name="CheckBox">
<item name="colorControlNormal">@color/light_grey</item>
<item name="colorControlActivated">@color/ts_white</item>
</style>
and use it like this:并像这样使用它:
<androidx.appcompat.widget.AppCompatCheckBox
.....
android:theme="@style/Bonus.CheckBox"
..... />
after setting style you wont going to need to check views background programmaticaly设置样式后,您将不需要以编程方式检查视图背景
try this:尝试这个:
final CheckBox cbox= (CheckBox) findViewById(R.id.cb_flash);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Set the Flash CheckBox Text Color
cbox.setTextColor(Color.BLUE);
// Set the Flash CheckBox Background Color
cbox.setBackgroundColor(Color.parseColor("#cbff75"));
}
});
if(checkbox.isChecked()){
cbox.setTextColor(Color.BLUE);
// Set the Flash CheckBox Background Color
cbox.setBackgroundColor(Color.parseColor("#cbff75"));
}else{
//chenge color
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.