问题
I have a Linear layout with 3 text views. I want to change the text view's font color when any of the text view is selected. I would like to retain the new color until another text view is selected. Basically this linear layout should mimic check box's selection behavior. I would like to use the selector to change the color of the text view's font color.
I used the below selector on text view's textColor and this only changes the font color as long as the text view is pressed
android:textColor="@drawable/selector_header_text"
xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue"></item>
<item android:state_focused="true" android:color="@color/blue"/>
<item android:color="@color/light_gray"></item>
</selector>
How can I make my text views in linear layout to retain text color as long as its selected and not just pressed ?
回答1:
add the code android:state_selected="true" to your selector file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue"></item>
<item android:state_focused="true" android:color="@color/blue"/>
<item android:state_selected="true" android:color="@color/blue"/>
<item android:color="@color/light_gray"></item>
</selector>
In your class file add the following code,
textView.setselected(true);
回答2:
Use android:state_checked, in addition or instead of android:state_pressed.
来源:https://stackoverflow.com/questions/18783407/how-to-change-text-color-on-press-of-a-particular-textview-in-a-linearlayout