更改复选框对象的颜色

时间:2018-06-29 14:26:53

标签: java android

当前复选框的颜色是蓝色,但是我想禁用该复选框,并在特定条件变为真时将其颜色更改为暗灰色。我找不到通过其对象更改复选框颜色的方法。

2 个答案:

答案 0 :(得分:0)

在条件为真时,只需编程设置按钮提示即可。尝试这些行

int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));

答案 1 :(得分:0)

只需在代码中实现这两件事

styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">#000</item>   <!-- normal border color change as you wish -->
    <item name="colorControlActivated">#000</item> <!-- activated color change as you wish -->
    <item name="android:textColor">#FFFF3F3C</item> <!-- checkbox text color -->
</style>

现在位于CheckBox代码下方的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 -->