java - 单击时 TextView 颜色不会改变

标签 java android colors textview

我正在使用选择器在用户触摸它时更改 TextView 颜色。但颜色是永久的黑色,触摸时不会改变。这是我的代码:

@color/red.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
<item android:color="#000000" />

TextView :

 <TextView
    android:id="@+id/delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="59dp"
    android:layout_y="449dp"
    android:text="Delete"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/red"
    android:textSize="20dp" />

解决方案:对textview使用setOnClickListener()而不是setOnTouchListener()。

最佳答案

编辑

<item android:drawable="@drawable/color_white" android:state_pressed="true"></item>
<item android:drawable="@drawable/color_black"></item>

在 color.xml 中

<drawable name="color_black">#000000</drawable>
<drawable name="color_white">#ffffff</drawable>

关于java - 单击时 TextView 颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20213951/

相关文章:

java - 如果我第二次更改位置,上一个标记也会显示

C++:读取位图错误

colors - 假设红、蓝、黄为原色,计算混合色的 RGB 代码

Java象棋 table

java - 在 JLabel 上的 gif 和 png 之间切换

java - 如何序列化 Jackson Joda 日期格式?

java - 删除广告时调整布局大小

android - 如何自定义安卓锁屏?

android - XML 布局 : explicit attributes or styles. xml 的首选方法?

java - android 中的错误 - SQLite