单击时TextView颜色不会更改

时间:2013-11-26 10:08:27

标签: java android colors textview

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

@颜色/ 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()。

3 个答案:

答案 0 :(得分:1)

修改

<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>

答案 1 :(得分:0)

尝试

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

答案 2 :(得分:0)

嗨,请尝试这可能是有用的

<item android:state_selected="true" android:color="#ffffff" />
<item android:state_focused="true" android:color="#000000" />
<item android:state_pressed="true" android:color="#000000" />
<item android:color="#000000" />