简体   繁体   English

Android - Textview在改变状态时改变颜色

[英]Android - Textview change color on changing of state

How can I apply color on the various states(focused, pressed, enabled) of the TextView? 如何在TextView的各种状态(聚焦,按下,启用)上应用颜色?

I have already referred this: http://developer.android.com/reference/android/content/res/ColorStateList.html , but do not know how can I apply color state list to the TextView? 我已经提到了这个: http//developer.android.com/reference/android/content/res/ColorStateList.html ,但不知道如何将颜色状态列表应用于TextView? or is there any other way to do so ? 或者还有其他方法吗?

Update: 更新:

I want to change the Background color. 我想改变背景颜色。

create xml under res/color dir. 在res / color目录下创建xml。

example file name : selector_white_gray.xml 示例文件名:selector_white_gray.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="@color/Gray"/> <!-- pressed -->
    <item android:color="@color/White"/> <!-- default -->
</selector>

you can add more states. 你可以添加更多的状态。 you can use color code like "#ffffff" instead of predefined "@color/White". 您可以使用“#ffffff”等颜色代码而不是预定义的“@ color / White”。 Becarefull, use android:color not android:drawable. Becarefull,使用android:color not android:drawable。 this example changes color of text when pressed on it. 此示例在按下文本时更改文本的颜色。 set the textColor attribute to selector above. 将textColor属性设置为上面的选择器。

<TextView
       android:layout_width="wrap_content"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       android:textColor="@color/selector_white_gray"
       android:textSize="18sp" 
       android:textStyle="bold" >
</TextView>

Create new a new xml (in the drawable folder). 创建一个新的xml (在drawable文件夹中)。 with the color you can specify image for each event state 使用颜色,您可以为每个事件状态指定图像
and you can you can set this xml as you background 你可以设置这个xml作为背景

if your xml is ' res/drawable/abc.xml ' then set background as 如果你的xml是' res/drawable/abc.xml ',那么将background设置为

android:background="@drawable/abc"

Edited to add color in state xml 编辑为在状态xml中添加颜色
our xml, res/drawable/abc.xml 我们的xml, res/drawable/abc.xml

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

Then declare gray in your res\\values\\strings.xml 然后在your res\\values\\strings.xml声明为灰色

<color name="gray">#808080</color>
textView = (TextView)findViewById(R.id.myTextView);
mMainView.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        textView.setTextColor(Color.GREEN);//set the color here
    }

});

If you want to change color of the text - you create it as an xml in res/color folder ( for example res/color/mycolor.xml and then in your TextView you assig it color as android:textColor="@color/mycolor" 如果你想改变文本的颜色 - 你将它创建为res / color文件夹中的xml(例如res / color / mycolor.xml,然后在你的TextView中你将其颜色设置为android:textColor="@color/mycolor"

For changing background see other answer. 更改背景请参阅其他答案。

It's easy. 这很简单。 Just intercept desired event and write smth like: 只需拦截所需的事件并写下如下:

TextView textView=(TextView)findViewById(R.id.myText);
String s=getString(R.string.myText);
SpannableString ss=new SpannableString(s);
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM