繁体   English   中英

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

[英]How to change checkbox checked color programmatically

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

这是我的代码:

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

    }
});

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

在此处输入图片说明

AppCompatCheckBox替换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);
}

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

在 kotlin 中,你可以这样写:

refBtn.buttonTintList = ColorStateList.valueOf(Color.GRAY)

您可以通过所需的颜色更改更改 'Color.GRAY',或者编写 Color.rgb() 并定义颜色:)

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

这是 kotlin 扩展

fun AppCompatCheckBox.setTintAuto(enabledColor : Int, disabledColor : Int) {

val colorTint = ColorStateList(
    arrayOf(
        intArrayOf(-R.attr.state_checked),
        intArrayOf(R.attr.state_checked)
    ), intArrayOf(enabledColor , disabledColor )
)
this.buttonTintList = colorTint 
this.setTextColor(colorTint )
}

然后使用它

checkBox.setTintAuto(activeColor,disabledColor)

快乐编码

您是否尝试创建selector并将此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>

在布局文件中,将此文件应用于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/checked@drawable/unchecked是你的复选框的两个图像,所以你可以放一个你想要的颜色

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

android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM