Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改复选框对象的颜色_Java_Android - Fatal编程技术网

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 -->