更改未识别的单选按钮的文本颜色

时间:2017-03-01 16:47:11

标签: java android android-studio radio-button

我有一个带4个单选按钮的广播组。我使用这段代码:

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

检索已检查单选按钮的索引。

问题是我想要更改已选中单选按钮的文本颜色,而我每次都不知道具体的单选按钮。所以:checkedRadioButton.setTextColor(color); 向我显示一个错误,我需要'添加限定符',基本上告诉我,我应该使用特定的单选按钮来调用该方法,如:

radioButtonAns1.setTextColor(color);

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

提前致谢!

3 个答案:

答案 0 :(得分:1)

如果您将找到的setTextColor投放到View,则可以使用RadioButton方法:

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

答案 1 :(得分:0)

嗯它有点直接,错误基本上是嘿你试图改变包含单选按钮组的组的颜色。请先选择你想要的那个然后改变它。要解决这个问题,请声明一个布尔数组,然后设置一个on click侦听器来检测单击的单选按钮的位置,然后设置然后在数组中将Boolean值设置为true。

答案 2 :(得分: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>
相关问题