Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

android - How to change color of the text in TextView on hover like in css?

How to change color of the text in TextView on hover like in css with selector? I know for button to define selector with items

<item
    android:drawable="@drawable/down"
    android:state_pressed="true"
    android:state_enabled="true">
</item>
<item
    android:drawable="@drawable/up"
    android:state_focused="true"
    android:state_enabled="true">
</item>

but I need for TextView textColor, but item doesn't recognize that attribute. Is there way to do this from xml and not from code ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add that selector as a resource file (res/color/text_color.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/focused_text_color"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="@color/pressed_text_color" />
    <item android:state_enabled="false" android:color="@color/disabled_text_color" />
    <item android:color="@color/default_text_color"/>
</selector>

And use it:

android:textColor="@color/text_color"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...