2016-07-25 6 views
1

Ich habe ein Problem, ich kann die Textfarbe nicht ändern, wenn RadioButton ausgewählt ist. Ich habe dies in activity.xmlFarbtext einstellen RadioButton hat RadioGroup mit benutzerdefinierter Form eingecheckt

<RadioGroup 
    android:id="@+id/activity_onsite" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" > 
    <RadioButton 
     android:id="@+id/onsite_0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:background="@drawable/toggle_btn" 
     android:padding="10dp" 
     android:text="office" 

     android:button="@null"/> 

    <RadioButton 
     android:id="@+id/onsite_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/toggle_btn" 
     android:text="clients" 
     android:padding="10dp" 
     android:button="@null"/> 
</RadioGroup> 

in meine Ressourcen @ ziehbar/togglebtn Ich habe

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/radio_btn_selected" android:state_checked="true" /> 
    <item android:drawable="@drawable/radio_btn_regular" android:state_checked="false"/> 
</selector> 

Und schließlich in meinem @ ziehbar/radio_btn_selected

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
<corners 
    android:radius="5dp" /> 
<solid 
    android:color="#39b3d7" 

    /> 
<stroke 
    android:width="1dp" 
    android:color="#269abc" /> 
</shape> 

Und in @ ziehbar/radio_btn_regular

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
<corners 
    android:radius="5dp" /> 
<solid 
    android:color="#fff" 

    /> 
<stroke 
    android:width="1dp" 
    android:color="#269abc" /> 
</shape> 

Kann ich Textfarbe in radio_btn_selected.xml ändern?

+0

Sie benutzerdefinierte Farbwähler erstellen und die Textfarbe mit diesem Set – lal

+0

http://stackoverflow.com/a/3565624/3092341 das kann Ihnen helfen – lal

Antwort

4

Sie können beide Textattribute für den Hintergrund & nicht mit einem einzelnen Selektor festlegen. Erstellen Sie einen neuen Selektor für Textfarbe sagen radio_text_selected.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#39b3d7"/> 
    <item android:state_checked="true" android:color="#39b3d7"/> 
    <item android:color="#ff8a8a"/> 
</selector> 

die Textfarbe Set Attribut RadioButton-

android:textColor="@drawable/radio_text_selected" 
+0

Vielen Dank! es funktioniert! unten werde ich die Lösung posten. – LorenzoBerti

+0

Das funktioniert wie ein Zauber, vielen Dank. Msgstr "Sie können nicht beide Attribute background und textColor mit einem einzigen Selektor setzen." Dieses Konzept löst auch viele andere Probleme. – MarsAndBack

+0

das ist Arbeit aber nur einmal –

0

@sJy thankyou. Es funktioniert. Ich habe ein radio_text_selected.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:color="#f3f3"/> 
    <item android:state_checked="false" android:color="#000"/> 
</selector> 

und auf meinem RadioButton- in Activity.xml hinzugefügt:

<RadioGroup 
    android:id="@+id/activity_onsite" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" > 
    <RadioButton 
     . 
     . 
     android:textColor="@drawable/radio_text_selected"/> 
    <RadioButton 
     . 
     . 
     android:textColor="@drawable/radio_text_selected"/> 
</RadioGroup> 
Verwandte Themen