Android: 以编程方式更改单选按钮和复选框的颜色
Android: Change the color of RadioButtons and checkboxes programmatically
我以编程方式在 LinearLayout
中创建了 RadioButton
和 CheckBox
。但是,现在我想更改单选按钮的颜色和复选框的颜色。我用
RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));
checkbox.setHighlightColor(Color.parseColor("#0c83bd"));
但是没有用。
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"));
}
});
}
试试这个
AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
而不是
RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
使用 AppCompatCheckBox 和 AppCompatRadioButton 而不是 CheckBox 和 RadioButton。
您的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);
对于 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
中声明的属性并更改代码。
您可以像这样创建动态 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));
我继续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);
}
我已经尝试了几种方法来以编程方式实现它,但 none 除了以下内容之外对我有用。放在这里,以防有人觉得有用。
<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 class 中的 setButtonTintList 来设置这个 colorStateList
CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())
尝试添加这一行:
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" />
我以编程方式在 LinearLayout
中创建了 RadioButton
和 CheckBox
。但是,现在我想更改单选按钮的颜色和复选框的颜色。我用
RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));
checkbox.setHighlightColor(Color.parseColor("#0c83bd"));
但是没有用。
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"));
}
});
}
试试这个
AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
而不是
RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
使用 AppCompatCheckBox 和 AppCompatRadioButton 而不是 CheckBox 和 RadioButton。 您的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);
对于 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
中声明的属性并更改代码。
您可以像这样创建动态 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));
我继续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);
}
我已经尝试了几种方法来以编程方式实现它,但 none 除了以下内容之外对我有用。放在这里,以防有人觉得有用。
<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 class 中的 setButtonTintList 来设置这个 colorStateList
CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())
尝试添加这一行:
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" />