选择时,RadioButton颜色和文本颜色会发生变化

时间:2016-09-16 11:36:52

标签: android android-studio checkbox colors radio-button

我该怎么做?

enter image description here

这是我用来改变文字颜色的方法:

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));
                }
            }
        });

我设置了<item name="android:buttonTint">@color/colorAccent</item>,但未选中的单选按钮是colorAccent而不是lightGray

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

要在select / checked / focused时更改文本颜色,请在android:textColor for widget中使用此colorList:

<?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>

并且按钮的背景在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>

注意在文本中使用color1作为checked_state,在背景中使用unchecked_state作为对比..

我希望这可能有所帮助,&#39;。

答案 1 :(得分:2)

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>

使用两个drawables来选择和未选择单选按钮的状态

并使用上面的选择器:

<!--   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" />