【问题标题】:How to change checkbox checked color programmatically如何以编程方式更改复选框选中的颜色
【发布时间】:2016-03-24 06:08:33
【问题描述】:

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

    }
});

问题在于它没有改变正确的事情。有关如何更改此颜色的任何想法?

【问题讨论】:

    标签: java android checkbox colors


    【解决方案1】:

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

    【讨论】:

    • 其实第一个分支自己就够了。完全不需要检查 SDK 版本。
    【解决方案2】:

    将您的CheckBox 替换为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);
    }
    

    【讨论】:

    • 最后一行应该是'CompoundButtonCompat.setButtonTintList(checkBox, colorStateList)'
    • 如果不起作用,请将uncheckedColorContextCompat.getColor(checkBox.getContext, uncheckedColor) 包裹起来,并与checkedColor 相同
    【解决方案3】:

    在 kotlin 中你可以这样写:

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

    你用你想要的颜色改变'Color.GRAY',或者写Color.rgb()并定义颜色:)

    【讨论】:

      【解决方案4】:
      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" />
      

      【讨论】:

        【解决方案5】:

        这是 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)
        

        【讨论】:

          【解决方案6】:

          您是否尝试过创建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
              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"
          

          【讨论】:

          • 此解决方案不适用于 API 级别 17(以及可能低于 API 21 的任何内容)
          猜你喜欢
          • 1970-01-01
          • 2015-04-24
          • 2015-12-05
          • 1970-01-01
          • 2019-05-20
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          相关资源
          最近更新 更多