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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34340256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:47:17  来源:igfitidea点击:

How to change checkbox checked color programmatically

javaandroidcheckboxcolors

提问by user112016322

I'm using the CheckBox view in Android. I would like to change the color of it when its checked. Right now its that default dark green color when its checked and I would like to change it to something different and when not checked, just be the default colors.

我在 Android 中使用 CheckBox 视图。当它被选中时,我想改变它的颜色。现在它是默认的深绿色,当它被选中时,我想把它改成不同的东西,当没有选中时,只是默认颜色。

Here's my code:

这是我的代码:

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);
        }

    }
});

The problem is that it does not change the right thing. Any ideas on how to change this color?

问题是它并没有改变正确的事情。关于如何改变这种颜色的任何想法?

enter image description here

在此处输入图片说明

采纳答案by Gabriella Angelova

Did you try to create a selectorand assign this selectorto your CheckBoxlike this for example:

您是否尝试创建一个selector并将其分配selector给您CheckBox,例如:

//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>

In your layout file apply this file to your checkBox

在您的布局文件中,将此文件应用于您的复选框

<CheckBox
    android:id="@+id/myCheckBox"
    android:text="My CheckBox"
    android:button="@drawable/cb_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

@drawable/checkedand @drawable/uncheckedare two images of your checkbox, so you could put there a color that you want

@drawable/checked@drawable/unchecked是你的复选框的两幅图像,所以你可以把有颜色要

OR without changing the button layout, with adding this attribute to your checkbox

或不更改按钮布局,将此属性添加到您的复选框

android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE"

回答by ywwynm

Replace your CheckBoxwith AppCompatCheckBoxand call following method:

将您替换CheckBoxAppCompatCheckBox并调用以下方法:

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);
}

回答by sud007

To tint the CompoundButton Tints try this, for Both the API>21 and below.

要为 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
}

回答by Amit Sinha

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" />