Android:如何以编程方式更改RadioButton和复选框的颜色

18

我通过编程方式在 LinearLayout 中创建了 RadioButtonCheckBox,但现在我想更改单选按钮和复选框的颜色。我使用以下代码:

RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));

checkbox.setHighlightColor(Color.parseColor("#0c83bd"));

但是它并没有生效。


使用背景图片 - Androider
但我想设置颜色而不是附加图像。 - Amit Desale
在Android支持库中是否有可用的函数? - Androider
8个回答

22

请使用 AppCompatCheckBoxAppCompatRadioButton 代替 CheckBoxRadioButton您的xml将包含:

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cbSelected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorAccent" //This to set your default button color
    android:checked="true"/>

<android.support.v7.widget.AppCompatRadioButton
    android:id="@+id/rb"
    app:buttonTint="@color/colorAccent" //This to set your default button color
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

现在是Java代码部分: 创建 ColorStateList

        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {getResources().getColor(R.color.colorPrimary) }
        );

要以编程方式更改 AppCompatRadioButton AppCompatCheckBox 的颜色,请调用 setSupportButtonTintList

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);

16

您可以这样创建动态的RadioButton

RadioButton male = new RadioButton(ReservationContact.this);
male.setTextColor(Color.BLACK);
male.setText("Male");
male.setSelected(true);
male.setId(0);

这用于更改RadioButton圆圈的默认颜色。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));

14

试试这个

AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);

替代

RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);

7

对于CheckBox,请将您的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);
}

我认为对于RadioButton也应该是类似的。您可以在android.R.attr中检查声明的属性并更改代码。


1

我尝试了几种编程方法来实现它,但除了以下方法外,都没有成功。在这里放下来,以防有人觉得有用。

<androidx.appcompat.widget.AppCompatRadioButton
    android:id="@+id/radio_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null" />

创建一个ColorStateList。
private fun getRadioButtonColor(): ColorStateList {
    val states = arrayOf(
            intArrayOf(-android.R.attr.state_checked),
            intArrayOf(android.R.attr.state_checked))

    val colors = intArrayOf(
            ContextCompat.getColor(context, R.color.grey2),
            ContextCompat.getColor(context, R.color.actionBlue)
    )

    return ColorStateList(states, colors)
}

然后使用CompoutButtonCompat类中的setButtonTintList方法来设置这个colorStateList。

CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())

0
尝试添加这行代码:
app:buttonTint="@color/yellow"

你的 xml 示例:

 <RadioButton
      android:id="@+id/radio"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      app:buttonTint="@color/yellow"
      android:text=" Radio"
      android:textColor="@color/yellow" />

0

我在继续回答ywwynm的问题。

Google将setSupportButtonTintList设置为受限,因此无法使用它。

解决方法是将按钮作为TintableCompoundButton接口使用,该方法未受限制。

适用于API 19+的AppCompatRadioButton。 AppCompatCheckbox实现了相同的接口,理论上也应该可以工作,但我还没有测试过。

祝玩得愉快 :)

public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, 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
            }
    );
    ((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
}

-1
   RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
    rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            rb.setButtonDrawable(R.drawable.image);
            rb.setHighlightColor(Color.parseColor("#0c83bd"));


        }
    });
  }

如果用户设置了自己的OnCheckedChangeListener怎么办? - Emad Razavi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接