Java 更改复选框对象的颜色
当前复选框的颜色为蓝色,但我想禁用该复选框,并在特定条件变为真时将其颜色更改为暗灰色。我找不到通过其对象更改复选框颜色的方法。当条件为真时,您只需通过编程方式设置按钮。.尝试以下几行Java 更改复选框对象的颜色,java,android,Java,Android,当前复选框的颜色为蓝色,但我想禁用该复选框,并在特定条件变为真时将其颜色更改为暗灰色。我找不到通过其对象更改复选框颜色的方法。当条件为真时,您只需通过编程方式设置按钮。.尝试以下几行 int states[][] = {{android.R.attr.state_checked}, {}}; int colors[] = {color_for_state_checked, color_for_state_normal} CompoundButtonCompat.setButtonTintList
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));
只需在代码中实现这两件事 styles.xml 现在,在您的main_activity.xml中,将复选框代码放在下面
<CheckBox android:id="@+id/check_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I agree"
android:theme="@style/MyCheckBox"/> <!-- here apply your checkbox style -->
你能解释一下你创建的这个新对象吗?新ColorStateListState,colorsobject颜色包含选中状态时定义的颜色,以及未选中状态时定义的颜色u,states对象包含要更改其颜色的属性
<CheckBox android:id="@+id/check_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I agree"
android:theme="@style/MyCheckBox"/> <!-- here apply your checkbox style -->