简体   繁体   English

如何在Android中的TextView所选项目中更改永久文本颜色?

[英]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.

相关问题 如何根据所选颜色(从颜色列表中选择)更改android中TextView文本的颜色 - How to change the color of text of TextView in android according to chosen color, selected from a list of color 在 Android Spinner 上将文本颜色更改为所选项目 - Change text color to selected item on Android Spinner 在Android中更改Listview所选项目的文本颜色 - Change text color of selected item of Listview in Android 在表格布局Android中选择行时,更改TextView的文本颜色 - change the Text color of TextView when the row is selected in Table Layout Android 如何在Android中的TextView中更改文本颜色? - How to change the text color in a TextView in Android? 如何在TextView中更改文本的颜色 - How do I change color of text in TextView Android:如何更改首选项中的所选项目颜色? - Android: how to change selected item color in Preference? 如何更改所选Textview的颜色 - How change the color of selected Textview Android:在Java中更改“选定列表项”的TEXT颜色 - Android:change the the TEXT color of Selected list Item in Java 更改 RecyclerView 上仅选定项目的文本颜色 android Kotlin - Change text color of only selected item on RecyclerView android Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM