[英]RadioButton color and text color change when selected
我怎樣才能做到這一點?
這是我用來更改文本顏色的方法:
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
有任何想法嗎?
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" />
要在選擇/選中/聚焦時更改文本顏色,請在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以便形成對比。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.