在安卓中更改复选框的背景颜色

13

我需要开发一个应用程序,在这个程序中要使用复选框。当我选择了复选框时,它的默认背景颜色是黄色,但我希望能使用渐变来改变已选和未选状态下的背景颜色。请问我该如何实现?以下是我的当前代码:

 <CheckBox
   android:id="@+id/rempasswordcheckbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/passwordview"

   android:layout_y="200dp"
   android:paddingLeft="45dp"
   android:text="Remember Password!"
   android:textColor="#1d2328" />

1
使用素材时,建议使用纯色(平面)而不是渐变色。如果您不介意的话,设置 android:buttonTint="@color/mybrown" 是更改复选框颜色的简单方法。 - shauvik
6个回答

12

如果你想改变复选框(按钮)的背景颜色,请使用以下代码:

mcheckbox.setButtonDrawable(R.drawable.someotherbackground);

在这里,someotherbackground是drawable文件夹中的图像,它是你想要更改复选框背景的图像。

尝试以下代码:

 mcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {

                System.out.println("checked" + isChecked);
                mcheckbox.setButtonDrawable(R.drawable.imageWhenActive);
                    System.out.println("app constant is set as "+isChecked);
            }
            else
            {
                mcheckbox.setButtonDrawable(R.drawable.imageWheninactive);
                System.out.println("app constant is set as "+isChecked);
            }

        }
    });

7

res/drawable/checkbox_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true">
        <shape>
            <gradient android:startColor="#FFFFFF" android:endColor="#000000" android:angle="-90"/>
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:startColor="#000000" android:endColor="#FFFFFF" android:angle="-90"/>
        </shape>
    </item>
</selector>

在您的布局中:

<CheckBox ...
    android:button="@drawable/checkbox_background" />

如果您想使用现有的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/checked_drawable" />
    <item android:drawable="@drawable/unchecked_drawable" />
</selector>

复选框无法转换为ImageView?为什么会发生这种情况? - chhameed

4

使用代码。

checkBox.setBackgroundColor(Color.BLUE);

代码

CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
     @Override
     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) 
     {
         // TODO Auto-generated method stub
         if (buttonView.isChecked()) 
         {
             //cb.setBackgroundColor(Color.BLUE);
             cb.setBackgroundColor(Color.parseColor("#FFFFFF"));
         }
         else
         {
             // Not Checked
             // Set Your Default Color. 
         }
     }
}); 

我必须在选中和未选中条件下使用渐变颜色值,请给我解决方案。 - user1676640
@user1676640 你可以使用Color.parseColor("#FFFFFF")。 - Chirag

4

尝试使用这段代码

public class MainActivity extends Activity {

CheckBox box;
boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    box=(CheckBox)findViewById(R.id.box);

    box.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if(flag){
                GradientDrawable d=new GradientDrawable();
                d.setColor(Color.RED);
                box.setBackgroundDrawable(d);
                }
            else{
                GradientDrawable d=new GradientDrawable();
                d.setColor(Color.GREEN);
                box.setBackgroundDrawable(d);
            }
            flag=!flag;
            }

    });
}

}


如果您使用的是Jellybean或更高版本的SDK,则请使用setBackground()方法,否则请使用setBackgroundDrawable()方法。 - Android Developer

1
改变主题的 colorAccent。
  <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/orange</item>
    ...
  </style>

1
请在您的复选框 XML 中使用以下代码:
<CheckBox
   android:id="@+id/rempasswordcheckbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/passwordview"

   android:background="#0000FF"

   android:layout_y="200dp"
   android:paddingLeft="45dp"
   android:text="Remember Password!"
   android:textColor="#1d2328" />

未生效。以这种方式放置时,按钮消失了。 - Shad
@Shad,能否具体说明你的问题是什么? - Android Boy
在我的情况下,我需要更改复选框按钮的背景,而不是整个组件的背景。这种方式无法工作,因为似乎复选框状态由系统图像表示。我们需要像Yusyuriv上面所做的那样:创建一个形状然后在其中涂上颜色。或者创建另一个图像以覆盖原始可绘制图像。 - Shad
@Shad,你的问题有些不同。因此,你应该创建另一个问题线程。 - Android Boy
这会改变整个带有文本的块的背景,但不会改变带有旗帜的小正方形的背景。 - CoolMind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接