简体   繁体   English

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

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

I build an app with multi calculation system and it need every thing filled to get the result, so if the user left the checkbox without selection i would to inform him by making the text and the box of the checkbox change to "Red" like the picture in the link: 我使用多计算系统构建一个应用程序,它需要填充所有内容以获得结果,因此如果用户离开checkbox而没有选择,我会通过将文本和复选框的checkbox更改为“红色”来通知他,如同链接中的图片:

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

if we assume my checkbox is "Yes", so how to make code for this and another one to rerun it back to it's original one? 如果我们假设我的复选框是“是”,那么如何为此创建代码,另一个代码重新运行回原来的那个?


I can make an custom xml file like this: 我可以像这样制作一个自定义的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"/>

I know how to make the condition of checking: 我知道如何制定检查条件:

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

but in this case i need two somethings: 但在这种情况下,我需要两个东西:

1- checkbox image like the default one on android, then making it's box "red" 1-复选框图像像android上的默认图像,然后使它的框“红色”

2- I need a code to make it return back to default one with black text and box 2-我需要一个代码,使其返回默认的黑色文本和框

,, So how can I do that? 那我该怎么办呢? or if anyone have another solution for my problem? 或者如果有人有我的问题的另一种解决方案?

can create another set of xml called custom_red_checkbox.xml 可以创建另一组名为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"/>

and your code modify to 并且您的代码修改为

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