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

Ami*_*ale 11 checkbox android colors radio-button

我以编程方式创建RadioButton并创建.但是,现在我想更改单选按钮的颜色和复选框的颜色.我用CheckBoxLinearLayout

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

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

但它没有用.

Ran*_*ngh 19

使用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"/>
Run Code Online (Sandbox Code Playgroud)

现在为java代码:创建ColorStateList

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

要以编程方式更改AppCompatRadioButtonAppCompatCheckBox,请调用setSupportButtonTintList.

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

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);
Run Code Online (Sandbox Code Playgroud)


Aks*_*hay 13

试试这个

AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
Run Code Online (Sandbox Code Playgroud)

绝缘

RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
Run Code Online (Sandbox Code Playgroud)


小智 11

您可以RadioButton像这样创建动态:

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

这用于更改圆的默认颜色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));
Run Code Online (Sandbox Code Playgroud)


yww*_*ynm 5

对于CheckBox,将您替换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);
}
Run Code Online (Sandbox Code Playgroud)

我认为它应该与RadioButton. 您可以检查声明的属性android.R.attr并更改代码。