在焦点上更改可点击的TextView的颜色并单击?

时间:2022-11-21 15:04:49

I have a clickable TextView that I want to give some colors to. But I don't know how. Here are the relevant code snippets from my two files that I'm working with:

我有一个可点击的TextView,我想给它一些颜色。但我不知道怎么做。以下是我正在使用的两个文件中的相关代码片段:

TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
title.setTextColor(R.color.textcolor);
title.setText(titleLine);
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true);
title.setId(idLine);
title.setFocusable(true);

title.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

                /* Irrelevant code */                   

    }
});

And this is my textcolor.xml file:

这是我的textcolor.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#000000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#000000"/> <!-- focused -->
    <item android:color="#000000"/> <!-- default -->
</selector>

When I use the textcolor-file by typing title.setTextColor(R.color.textcolor);, the textcolor just becomes grey, regardless if I press it or so. Which is strange since I have written "#000000" in all color fields.
But if I remove the setTextColor code, gets the textView a light grey color, and when I press it, it becomes black. But that aren't the colors that I want.

当我通过键入title.setTextColor(R.color.textcolor);来使用textcolor文件时,无论我是否按下它,文本颜色都会变为灰色。这很奇怪,因为我在所有颜色字段中都写了“#000000”。但是如果我删除了setTextColor代码,则将textView变为浅灰色,当我按下它时,它变为黑色。但那不是我想要的颜色。

So, can anyone help me with this problem?

那么,任何人都可以帮我解决这个问题吗?

Just to clarify: I want to be able to specify the colors for the text when it's normal, pressed and focused.

只是为了澄清:我希望能够在文本正常,按下和聚焦时指定文本的颜色。

8 个解决方案

#1


55  

If you want to set stateful color from code, you need to pass in ColorStateList as an argument to setTextColor passing an int to the method results in setting the color to all the states. It also looks like your xml is not totally correct. Example from ColorStateList docs looks like(should be located like this: res/color/selector_txt.xml):

如果要从代码中设置有状态颜色,则需要将ColorStateList作为参数传递给setTextColor,将int传递给方法会导致将颜色设置为所有状态。看起来你的xml也不完全正确。 ColorStateList docs的示例看起来像(应该像这样:res / color / selector_txt.xml):

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/testcolor1"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
    <item android:state_enabled="false" android:color="@color/testcolor3" />
    <item android:color="@color/testcolor5"/>
 </selector>

UPD on how to set a ColorStateList to text color:

关于如何将ColorStateList设置为文本颜色的UPD:

ColorStateList cl = null;
try {
   XmlResourceParser xpp = getResources().getXml(R.color.selector_txt);
   cl = ColorStateList.createFromXml(getResources(), xpp);
} catch (Exception e) {}

Note: The method createFromXml(Resources, XmlPullParser parser) was deprecated in API level 23. Use createFromXml(Resources, XmlPullParser parser, Theme)

注意:方法createFromXml(Resources,XmlPullParser解析器)在API级别23中已弃用。使用createFromXml(Resources,XmlPullParser解析器,主题)

With XML its as easy as:

使用XML,它很简单:

android:textColor="@color/selector_txt"

#2


42  

Step 1: Set the text color in xml like this

第1步:像这样在xml中设置文本颜色

android:textColor="@color/text_color"

Step2: Create res/color/text_color.xml

第2步:创建res / color / text_color.xml

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

#3


15  

Try this one.. It worked for me:

尝试这个..它对我有用:

File name: res/color/bg_tab_text_color.xml

文件名:res / color / bg_tab_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:state_selected="false" android:color="@color/tab_unselected_text_color"/>
    <item android:state_pressed="true" android:color="@color/tab_selected_text_color"/>
    <item android:state_pressed="false" android:state_selected="true" android:color="@color/tab_selected_text_color"/>
    <item android:color="@color/tab_unselected_text_color"></item>
</selector>

Try setting the color in xml layout as:

尝试将xml布局中的颜色设置为:

android:textColor="@color/bg_tab_text_color"

#4


10  

Look in R.java class (it's generated automatically). You have something like that:

查看R.java类(它是自动生成的)。你有类似的东西:

 public static final class color {
        public static final int gray_transparent=0x7f050001;
}

So in your code in line:

所以在你的代码中:

title.setTextColor(R.color.textcolor);

you're not setting values from textcolor.xml but int from R.java (which contains textcolor.xml address). The valid way to set color is:

您不是从textcolor.xml设置值,而是从R.java设置值(包含textcolor.xml地址)。设置颜色的有效方法是:

title.setTextColor(getResources().getColorStateList(R.color.textcolor));

#5


3  

It's very easy.Try this one.. It worked for me:

这很容易。尝试这个..它对我有用:

File name: res/color/bg_tab_text_color.xml

文件名:res / color / bg_tab_text_color.xml

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

<item android:state_pressed="true" android:drawable="@color/blue" />

Try setting the color in xml layout as:

尝试将xml布局中的颜色设置为:

android:textColor="@color/bg_tab_text_color"

#6


2  

Here is a very simple way programmatically:

这是一种非常简单的编程方式:

private void setColorStateList(TextView view) {
        int[][] states = new int[][] {
                new int[] { android.R.attr.state_pressed}, // pressed
                new int[] { android.R.attr.state_focused}, // focused
                new int[] { android.R.attr.state_enabled}  // enabled
        };

        int[] colors = new int[] {
                getResources().getColor(R.color.blue),
                getResources().getColor(R.color.green),
                getResources().getColor(R.color.green) 
        };

        ColorStateList list = new ColorStateList(states, colors);
        view.setTextColor(list);
        view.setClickable(true);
        view.setFocusableInTouchMode(true);
    }

#7


0  

In res/drawable/tab_textColor.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#FFFFFF" />
    <item android:state_pressed="true" android:color="#FFFFFF" />
    <item android:color="#CCCCCC" />
</selector>



<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/icon"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:text="@string/tab_indicator_home"
    android:textColor="@drawable/tab_textcolor"
    android:textSize="10sp"
    android:visibility="visible"
    tools:ignore="SmallSp" />

#8


0  

In res/color/text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white"
          android:state_selected="true"/>
    <item android:color="#8020e0"/>
</selector>

In layout:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Word"
    android:textColor="@color/text_selector"
    android:textSize="12sp"/>

In code:

textView.setSelected(false); // To deselect.
textView.setSelected(true);

#1


55  

If you want to set stateful color from code, you need to pass in ColorStateList as an argument to setTextColor passing an int to the method results in setting the color to all the states. It also looks like your xml is not totally correct. Example from ColorStateList docs looks like(should be located like this: res/color/selector_txt.xml):

如果要从代码中设置有状态颜色,则需要将ColorStateList作为参数传递给setTextColor,将int传递给方法会导致将颜色设置为所有状态。看起来你的xml也不完全正确。 ColorStateList docs的示例看起来像(应该像这样:res / color / selector_txt.xml):

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/testcolor1"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
    <item android:state_enabled="false" android:color="@color/testcolor3" />
    <item android:color="@color/testcolor5"/>
 </selector>

UPD on how to set a ColorStateList to text color:

关于如何将ColorStateList设置为文本颜色的UPD:

ColorStateList cl = null;
try {
   XmlResourceParser xpp = getResources().getXml(R.color.selector_txt);
   cl = ColorStateList.createFromXml(getResources(), xpp);
} catch (Exception e) {}

Note: The method createFromXml(Resources, XmlPullParser parser) was deprecated in API level 23. Use createFromXml(Resources, XmlPullParser parser, Theme)

注意:方法createFromXml(Resources,XmlPullParser解析器)在API级别23中已弃用。使用createFromXml(Resources,XmlPullParser解析器,主题)

With XML its as easy as:

使用XML,它很简单:

android:textColor="@color/selector_txt"

#2


42  

Step 1: Set the text color in xml like this

第1步:像这样在xml中设置文本颜色

android:textColor="@color/text_color"

Step2: Create res/color/text_color.xml

第2步:创建res / color / text_color.xml

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

#3


15  

Try this one.. It worked for me:

尝试这个..它对我有用:

File name: res/color/bg_tab_text_color.xml

文件名:res / color / bg_tab_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:state_selected="false" android:color="@color/tab_unselected_text_color"/>
    <item android:state_pressed="true" android:color="@color/tab_selected_text_color"/>
    <item android:state_pressed="false" android:state_selected="true" android:color="@color/tab_selected_text_color"/>
    <item android:color="@color/tab_unselected_text_color"></item>
</selector>

Try setting the color in xml layout as:

尝试将xml布局中的颜色设置为:

android:textColor="@color/bg_tab_text_color"

#4


10  

Look in R.java class (it's generated automatically). You have something like that:

查看R.java类(它是自动生成的)。你有类似的东西:

 public static final class color {
        public static final int gray_transparent=0x7f050001;
}

So in your code in line:

所以在你的代码中:

title.setTextColor(R.color.textcolor);

you're not setting values from textcolor.xml but int from R.java (which contains textcolor.xml address). The valid way to set color is:

您不是从textcolor.xml设置值,而是从R.java设置值(包含textcolor.xml地址)。设置颜色的有效方法是:

title.setTextColor(getResources().getColorStateList(R.color.textcolor));

#5


3  

It's very easy.Try this one.. It worked for me:

这很容易。尝试这个..它对我有用:

File name: res/color/bg_tab_text_color.xml

文件名:res / color / bg_tab_text_color.xml

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

<item android:state_pressed="true" android:drawable="@color/blue" />

Try setting the color in xml layout as:

尝试将xml布局中的颜色设置为:

android:textColor="@color/bg_tab_text_color"

#6


2  

Here is a very simple way programmatically:

这是一种非常简单的编程方式:

private void setColorStateList(TextView view) {
        int[][] states = new int[][] {
                new int[] { android.R.attr.state_pressed}, // pressed
                new int[] { android.R.attr.state_focused}, // focused
                new int[] { android.R.attr.state_enabled}  // enabled
        };

        int[] colors = new int[] {
                getResources().getColor(R.color.blue),
                getResources().getColor(R.color.green),
                getResources().getColor(R.color.green) 
        };

        ColorStateList list = new ColorStateList(states, colors);
        view.setTextColor(list);
        view.setClickable(true);
        view.setFocusableInTouchMode(true);
    }

#7


0  

In res/drawable/tab_textColor.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#FFFFFF" />
    <item android:state_pressed="true" android:color="#FFFFFF" />
    <item android:color="#CCCCCC" />
</selector>



<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/icon"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:text="@string/tab_indicator_home"
    android:textColor="@drawable/tab_textcolor"
    android:textSize="10sp"
    android:visibility="visible"
    tools:ignore="SmallSp" />

#8


0  

In res/color/text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white"
          android:state_selected="true"/>
    <item android:color="#8020e0"/>
</selector>

In layout:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Word"
    android:textColor="@color/text_selector"
    android:textSize="12sp"/>

In code:

textView.setSelected(false); // To deselect.
textView.setSelected(true);