简体   繁体   中英

How to do permanent text color change in TextView selected item in Android?

I want to make permanent color change on key press on a TextView.

Here is my color selector resides in res/color folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#a7524a"/>
<item android:state_focused="true" android:state_enabled="false" android:color="#a7524a"/>
<item android:state_pressed="true" android:color="#a7524a"/>
<item android:color="#595959"/>
</selector>

In TextView I refer it as

android:textColor="@color/bright_text_dark_focused"

It works fine. I want to make the color change permanent when user a presses a TextView item and releases. By default it goes to the default color on release.

Update:

I have changed the color selector as

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">      
<item android:state_focused="true" android:color="#a7524a"/>
<item android:state_pressed="true"  android:color="#a7524a" />   
<item android:color="#595959"/>    
</selector>

This is working as same before. I have added

android:textIsSelectable = "true"

property in the TextView . Then the background color on selection property disappears. But the text color becomes permanent when I click on the items. Any clue for why this happens? Actually I have this textview inside a listview. The background color property is from ListView.

I think if you added the line <item android:state_enabled="true" android:color="@color/bright_text_dark_focused"/> then enable the TextView on touch, it would stay. This is the case so long as you never disable it.

android:state_activated must solve your problem:

<item android:state_activated="true" android:color="#fff"/>

You may olso set this for your listView:

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

and when listView item clicked this:

mListView.setItemChecked(position, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM