繁体   English   中英

如何以编程方式更改我的复选框的文本和框颜色?

[英]How to change the text and the box color of my checkbox programmatically?

我使用多计算系统构建一个应用程序,它需要填充所有内容以获得结果,因此如果用户离开checkbox而没有选择,我会通过将文本和复选框的checkbox更改为“红色”来通知他,如同链接中的图片:

https://s15.postimg.org/kej9uc457/checkbox.jpg

如果我们假设我的复选框是“是”,那么如何为此创建代码,另一个代码重新运行回原来的那个?


我可以像这样制作一个自定义的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/option_unselected" android:state_checked="false" android:color="#ff0000"/>
<item android:drawable="@drawable/option_selected"   android:state_checked="true"/>

我知道如何制定检查条件:

if(yes.isChecked())
        {check.setButtonDrawable(mContext.getResources().getDrawable(
                R.drawable.custom_checkbox));}
else
        {check.setButtonDrawable(mContext.getResources().getDrawable(
                R.drawable.custom_checkbox));}

但在这种情况下,我需要两个东西:

1-复选框图像像android上的默认图像,然后使它的框“红色”

2-我需要一个代码,使其返回默认的黑色文本和框

那我该怎么办呢? 或者如果有人有我的问题的另一种解决方案?

可以创建另一组名为custom_red_checkbox.xml的xml

<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/option_unselected_red" android:state_checked="false" android:color="#ff0000"/>
<item android:drawable="@drawable/option_selected"   android:state_checked="true"/>

并且您的代码修改为

if(yes.isChecked())
        {check.setButtonDrawable(mContext.getResources().getDrawable(
                R.drawable.custom_checkbox));}
else
        {check.setButtonDrawable(mContext.getResources().getDrawable(
                R.drawable.custom_red_checkbox));}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM