[英]How to change the text and the box color of my checkbox programmatically?
我使用多計算系統構建一個應用程序,它需要填充所有內容以獲得結果,因此如果用戶離開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-我需要一個代碼,使其返回默認的黑色文本和框
那我該怎么辦呢? 或者如果有人有我的問題的另一種解決方案?
可以創建另一組名為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.