焦点更改点击TextView的颜色,然后点击?颜色、焦点、TextView

2023-09-12 07:23:34 作者:俄会微笑不代表一切都好。

我有,我想给一些颜色到一个可点击的TextView。但我不知道怎么办。以下是相关的code从我的两个文件我正在与片段:

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

    }
});

这是我的textcolor.xml文件:

And this is my textcolor.xml file:

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

当我键入使用文本颜色文件的 title.setTextColor(R.color.textcolor); 的,该文本颜色只是变成灰色,如果我不管preSS它左右。这是奇怪,因为我已经在所有的颜色领域写入#000000。 但是,如果我删除setTextColor code,得到TextView的浅灰色,当我preSS它,它变成了黑色。但是,这不是我想要的颜色。

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.

那么,谁能帮我解决这个问题?

So, can anyone help me with this problem?

只是为了澄清:我希望能够指定颜色的文本时,这是正常的,pressed和集中

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

推荐答案

如果你想设置从code状态的颜色,你需要传递ColorStateList作为参数传递给setTextColor路过一个int的方法使颜色设置的所有国家。它也像你的XML是不完全正确的。从ColorStateList文档举例如下:

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:

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

在如何设置ColorStateList的文本颜色UPD:

UPD on how to set a ColorStateList to text color:

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

使用XML的那么容易,因为:

With XML its as easy as:

android:textColor="@color/selector_txt"