在android中保存文本颜色

时间:2013-09-03 07:28:29

标签: android

我在布局中有一个TextView,点击文字我想改变颜色,我知道我可以使用text.setTextColor(Color.parseColor("#FFFFFF"));但问题是颜色没有保存并且在按下颜色后没有变了,我该怎么办?

3 个答案:

答案 0 :(得分:4)

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >

 <item android:state_pressed="true"
  android:color="#ffffffff"/> <!-- pressed -->
 <item android:state_focused="true"
  android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/>  <!--default -->

</selector>

答案 1 :(得分:3)

您可以为textView

创建一个选择器

http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList

或者您可以在ontouchlistener上注册button http://developer.android.com/reference/android/view/View.OnTouchListener.html

在MotionEvent的帮助下,您可以在按下时以及释放按钮时为动作切换案例

http://developer.android.com/reference/android/view/MotionEvent.html

答案 2 :(得分:0)

嘛!

您可以为所有4状态定义文本颜色,与为Button定义背景类似。例如:

文件名:/res/color/text_color.xml

保存在res / drawable / button.xml的XML文件:

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

将此文件放入按钮样式(\ res \ values \ styles.xml)中,如下所示:

<style name="button_text" parent="@android:style/Widget.Button">
 <item name="android:textColor">@color/text_color.xml</item> <!-- Here is specified text color --   >
</style>

现在将此样式添加到按钮

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/button_text">
</Button>

详细信息请查看this