【问题标题】:How to change the text and the box color of my checkbox programmatically?如何以编程方式更改复选框的文本和框颜色?
【发布时间】:2015-04-24 04:53:26
【问题描述】:

我构建了一个具有多计算系统的应用程序,它需要填写所有内容才能获得结果,因此如果用户在没有选择的情况下离开checkbox,我会通过制作checkbox 的文本和框来通知他更改为“红色” 就像链接中的图片:

如果我们假设我的复选框是“是”,那么如何为这个和另一个编写代码以重新运行它回到原来的那个?


我可以像这样制作一个自定义的 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- 我需要一个代码使它返回到带有黑色文本和框的默认代码

,, 那么我该怎么做呢?或者如果有人对我的问题有其他解决方案?

【问题讨论】:

标签: java javascript android checkbox


【解决方案1】:

可以创建另一组名为 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));}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2013-02-19
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多