如何在选择时更改TextView文本颜色?

and*_*ter 3 android textview

我的方案是当用户点击TextView文本时,它会将其颜色变为黑色到红色.我试图解决这个问题但没有发生任何事情.这是我的努力:我的TextView

 <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textSize="24sp"
        android:textColor="@color/text_selector"
        android:text="Shamima Sultana Shaumi"/>
Run Code Online (Sandbox Code Playgroud)

而text_selector类是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@android:color/red"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="@android:color/red" />
    <item android:state_enabled="false" android:color="@android:color/red" />
    <item android:color="@android:color/black"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

每当我点击时TextView,什么也没发生.我无法弄清楚我的问题是什么.

Bar*_*ski 6

选择器可以是例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/red" />
    <item android:color="@android:color/black"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

并将此OnClickListener用于textView:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        boolean isSelectedAfterClick = !v.isSelected();
        v.setSelected(isSelectedAfterClick);

        if (isSelectedAfterClick){
            //do something
        } else {
            //do something
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

相反的state_selected,isSelected并且setSelected可以使用(分别)state_activated,isActivatedsetActivated.