在焦点上更改可点击的 TextView 颜色并点击? [英] Change clickable TextView's color on focus and click?

查看:22
本文介绍了在焦点上更改可点击的 TextView 颜色并点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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); 使用 textcolor 文件时,无论我是否按下它,textcolor 都会变成灰色.这很奇怪,因为我在所有颜色字段中都写了#000000".
但是如果我删除 setTextColor 代码,textView 就会变成浅灰色,当我按下它时,它会变成黑色.但这不是我想要的颜色.

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.

推荐答案

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

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>

关于如何将 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) {}

注意:方法 createFromXml(Resources, XmlPullParser parser) 在 API 级别 23 中已弃用.使用 createFromXml(Resources, XmlPullParser parser, Theme)

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

使用 XML 就这么简单:

With XML its as easy as:

android:textColor="@color/selector_txt"

这篇关于在焦点上更改可点击的 TextView 颜色并点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆