[英]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. 我想在TextView上的按键上进行永久的颜色更改。
Here is my color selector resides in res/color folder. 这是我的颜色选择器,位于res / color文件夹中。
<?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 在TextView中,我将其称为
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.
我想在用户a按下TextView项目并释放时使颜色永久更改。 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 . 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.
实际上,我在列表视图中有此textview。 The background color property is from ListView.
背景颜色属性来自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. 我认为,如果您添加了
<item android:state_enabled="true" android:color="@color/bright_text_dark_focused"/>
然后启用了TextView
,它将保持<item android:state_enabled="true" android:color="@color/bright_text_dark_focused"/>
。 This is the case so long as you never disable it. 只要您从未禁用它,就是这种情况。
android:state_activated must solve your problem: android:state_activated必须解决您的问题:
<item android:state_activated="true" android:color="#fff"/>
You may olso set this for your listView: 您可以为您的listView设置此设置:
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and when listView item clicked this: 当listView项目单击时:
mListView.setItemChecked(position, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.