2017-03-01 79 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>