【问题标题】:Save text color in android在android中保存文本颜色
【发布时间】:2013-09-03 07:28:29
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:
     <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>
    

    【讨论】:

    • 它会导致这个错误java.lang.ClassNotFoundException: android.view.selector in loader dalvik.system.PathClassLoader[/data/app/com.emergencycalls-1.apk]
    • @你缺少使用的东西
    • 告诉我你到底在做什么?
    • it seys item tag需要drawable
    • 将上面的xml文件添加到你的texview android:drawable="@drawable/xmlfilename"
    【解决方案2】:

    你可以为你创建一个选择器textView

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

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

    在 MotionEvent 的帮助下,您可以在按下和释放按钮时切换动作

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

    【讨论】:

      【解决方案3】:

      好吧!

      您可以为所有 4 种状态定义文本颜色,就像为按钮定义背景一样。例如:

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

      XML 文件保存在 res/drawable/button.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>
      

      将此文件放入您的 Button Style(\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

      【讨论】:

      • 我想更改文本颜色而不是设置背景,在第一个解决方案中,您的代码需要图像作为背景
      • @AmirSadeghsorry 我错过了理解这个问题,现在我已经更新了答案。我一定会帮助你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2013-12-31
      • 2012-04-06
      • 1970-01-01
      • 2016-09-20
      • 2013-03-10
      相关资源
      最近更新 更多