Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

android - Change clickable TextView's color on focus and click?

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

    }
});

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>

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

With XML its as easy as:

android:textColor="@color/selector_txt"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...