2017-03-01 84 views
0

我有一个有4个单选按钮的收音机组。我使用此代码:更改未识别的单选按钮的文字颜色

int radioButtonID = radioGroup.getCheckedRadioButtonId(); 
View checkedRadioButton = radioGroup.findViewById(radioButtonID); 
int idx = radioGroup.indexOfChild(checkedRadioButton); 

检索选中的单选按钮的索引。

问题是,我想改变选中的单选按钮的文本颜色,而我不知道每次具体的单选按钮。所以:checkedRadioButton.setTextColor(color); 让我发现,我需要“添加一个限定词”,这basicaly正显示出我,我应该使用一个特定的单选按钮来调用该方法,就像一个错误:

radioButtonAns1.setTextColor(color);

我想如果有人也会解释为什么我有这个错误,如果有解决方案。唯一可以调用的方法是.setBackgroundColor(),看起来很丑。

在此先感谢!

RadioButton checkedRadioButton = (RadioButton) radioGroup.findViewById(radioButtonID); 
checkedRadioButton.setTextColor(color); 

回答

1

如果你投,你发现RadioButtonView可以使用setTextColor方法包含单选按钮组的组。请先选择你想要的那个,然后改变它。为了解决这个问题,声明一个布尔数组,然后设置一个点击监听器来检测单击的单选按钮的位置,然后设置布尔值为true。

0

那么它是有点儿直线前进的,误差基本上是说,嘿,你想改变的颜色:

+0

的事情是,我尝试访问的单选按钮,而不是无线电集团。我只需要指定我想要返回一个RadioButton而不是一个View。铸造它的伎俩 –

0

的XML替代方案是:

设置TextColor作为@Color选择器。

<RadioButton 
    android:id="@+id/radio_1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Radio 1" 
    android:textColor="@color/radiobuttonstate"/> 

颜色选择应该是:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#000000"/> 
    <item android:state_checked="true" android:color="#ffffff"/> 
    <item android:color="#00f"/> 
</selector>