【问题标题】:Set checkbox color with a color selector in Android在 Android 中使用颜色选择器设置复选框颜色
【发布时间】:2017-03-03 12:27:33
【问题描述】:

我正在使用颜色选择器在我的 android 应用程序中为 Checkbox 设置属性 buttonTint,以使其在选中/取消选中时改变颜色。

这是我的复选框样式的 XML:

<style name="standard_checkbox">
    <item name="android:layout_marginLeft">@dimen/checkbox_small_margin</item>
    <item name="android:layout_marginRight">@dimen/checkbox_small_margin</item>
    <item name="buttonTint">@color/checkbox_standard_selector</item>
</style>

这是我的选择器 XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/checkbox_checked" />
    <item android:state_checked="false" android:color="@color/checkbox_unchecked" />
</selector>

但它不能正常工作。 Checkbox 最初可见时具有正确的颜色(灰色)。单击/选中时,它会变成应有的绿色。但是当它再次被取消选中时,由于某种原因它仍然是绿色的。

编辑

问题也可能是我在清单中选择的主题引起的。我正在使用这个自定义主题:

<style name="Theme.Transparent.NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

【问题讨论】:

  • 检查和取消检查时如何处理颜色变化?
  • 用选择器,在复选框的不同状态上定义不同的颜色(android:state_checked=true/false)
  • 我的意思是onCheck改变了方法?
  • 哦,那......我没有在我的代码中使用 onchecklistener.. 当另一个按钮被按下时,我会根据复选框状态做一些事情。所以只调用“checkbox.isChecked()”,没有别的
  • 你为什么不使用android 命名空间buttonTint? android:buttonTint 风格?

标签: android android-view android-styles android-checkbox


【解决方案1】:

尝试以下代码:以编程方式更改 checkbox 按钮背景

CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) { 
            buttonView.setBackgroundColor(getResources().getColor(R.color.checked));
            }
        if (!isChecked) { 
            buttonView.setBackgroundColor(getResources().getColor(R.color.unchecked)); 
            }
    }
});

注意:getColor() 已弃用,您需要使用 ContextCompat.getColor(context, R.color.color)

第二种方式

使用您的颜色集创建一个颜色列表变量

ColorStateList  colorStateList = new ColorStateList(
        new int[][]{
                new int[]{-android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_checked} , // checked
        },
        new int[]{
                Color.parseColor("#FFFFFF"),  //unchecked color
                Color.parseColor("#009000"),  //checked color
        }
);

设置颜色使用:setButtonTintList()

 CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
 CompoundButtonCompat.setButtonTintList(cb,colorStateList);

【讨论】:

  • 某人在没有正当理由的情况下让fat brain 否决有效答案..继续做好工作..
  • 非常感谢!我不知道为什么有人投了反对票(不是我),但如果这是唯一的解决方案,我将不得不实现我自己的视图,扩展复选框并使用上面的代码。我会尝试一下并回复你。现在,请接受我的投票:)
  • thankx...这不是唯一的解决方案...它是一种解决问题的编程方式,适用于我的情况。
  • 对不起。这不像我想要的那样工作。这会更改复选框后面的背景颜色。我需要更改复选框本身的颜色。但是非常感谢您的努力
  • 是的,我发现了。但由于某种原因,这对我不起作用。我认为这与我选择的主题有关。但是,我找到了一个替代解决方案,结合了您的一些解决方案和我自己的一些解决方案。将其作为答案发布会更有意义,因为它是我特殊奇怪问题的解决方案。但我感谢您的帮助和努力,我的回答当然会归功于您。
【解决方案2】:

我和你有同样的问题,突然想到了。为什么不使用选择器。所以我尝试了自己,它成功了。

解决方案:

如果没有,请在 res 文件夹下创建颜色文件夹并放置此颜色选择器

checkbox_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:color="@color/colorPrimary" />
   <item android:state_checked="false" android:color="@color/colorSecondary" />
</selector>

在您的复选框内:

app:buttonTint="@color/checkbox_selector"

【讨论】:

  • 它不适合我.. 但谢谢。这可能与我选择的主题有关(请参阅我对问题的编辑)
【解决方案3】:

我根据rafsanahmad007 发布的一些代码找到了解决方案。我创建了一个带有侦听器的自定义复选框视图,根据自己的状态(选中与否)更改色调颜色。

我的自定义视图的代码:

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.CompoundButtonCompat;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CustomCheckBox extends CheckBox implements CompoundButton.OnCheckedChangeListener {

    private OnCheckedChangeListener listener;
    private int currentColor;

    public CustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOnCheckedChangeListener(this);
        setCheckboxColor();
    }

    private void setCheckboxColor() {
        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled},
                },
                new int[]{
                        getCurrentColor()
                }
        );
        CompoundButtonCompat.setButtonTintList(this, colorStateList);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        this.listener = listener;
    }

    public int getCurrentColor() {
        if (isChecked()) {
            return ContextCompat.getColor(getContext(), R.color.checkbox_checked);
        } else {
            return ContextCompat.getColor(getContext(), R.color.checkbox_unchecked);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setCheckboxColor();
        if (listener != null) {
            listener.onCheckedChanged(buttonView, isChecked);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多