[英]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
并且我设置了
<item name="android:buttonTint">@color/colorAccent</item>
,但是未选中的单选按钮是colorAccent
而不是lightGray
Any ideas? 有任何想法吗?
custom_radio_button.xml 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: 要在选择/选中/聚焦时更改文本颜色,请在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>
and for background of button use this in android:background: 对于按钮的背景,请在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.. 请注意,color1用作文本中的checked_state和用作背景的unchecked_state以便形成对比。
I hope that may help,'. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.