【问题标题】:RadioButton color and text color change when selected选中时单选按钮颜色和文本颜色会发生变化
【发布时间】:2017-01-24 15:02:57
【问题描述】:

我该怎么做?

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

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

有什么想法吗?

【问题讨论】:

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


【解决方案1】:

要在选择/选中/聚焦时更改文本颜色,请在 android:textColor 中为小部件使用此 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来做对比..

我希望这可能会有所帮助,'。

【讨论】:

  • 这是更改文本颜色的最佳方法。
【解决方案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>

对单选按钮的选中和未选中状态使用两个可绘制对象

并将上面的选择器用作:

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

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2014-09-04
    • 2013-10-20
    相关资源
    最近更新 更多