简体   繁体   中英

RadioButton color and text color change when selected

How can I do this?

在此处输入图片说明

This is what I use to change text color:

maleRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    maleRadioButton.setTextColor(getApplicationContext().getResources().getColor(R.color.colorPrimary));
                } else {
                    maleRadioButton.setTextColor(getApplicationContext().getResources().getColor(R.color.lightGray));
                }
            }
        });

and I set <item name="android:buttonTint">@color/colorAccent</item> , but the unselected radio button is colorAccent instead of lightGray

Any ideas?

custom_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">

         <item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
         <item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />

    </selector>

Use two drawables for selected and not selected state of the Radio Buttons

and use the above selector as:

<!--   Customized RadioButtons  -->


              <RadioButton
                   android:id="@+id/radioButtonCustomized1"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:text="Radio Button Selected"
                   android:layout_marginTop="20dp"
                   android:checked="true"
                   android:button="@drawable/custom_radio_button"
                   android:textSize="20dp" />

             <RadioButton
                   android:id="@+id/radioButtonCustomized2"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:text="Radio Button Not Selected"
                   android:layout_marginTop="10dp"
                   android:checked="false"
                   android:button="@drawable/custom_radio_button"
                   android:textSize="20dp" />

To change text color when select/checked/focused use this colorList in android:textColor for widget:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color1" android:state_focused="true" android:state_pressed="false"/>
    <item android:color="@color/color1" android:state_focused="true" android:state_pressed="true"/>
    <item android:color="@color/color1" android:state_focused="false" android:state_pressed="true"/>
    <item android:color="@color/color1" android:state_checked="true"/>
    <item android:color="@color/color2"/>
</selector>

and for background of button use this in android:background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/color2" android:state_checked="true"/>
    <item android:drawable="@color/color2" android:state_pressed="true"/>
    <item android:drawable="@color/color1" android:state_checked="false"/>
</selector>

Note the usage of color1 as checked_state in text and as unchecked_state for background to make contrast..

I hope that may help,'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM