android_5 修改一个textview中的字符串的颜色

例:Hello World, 欢迎观看《android教程》哈哈!

要求把《android教程》变红,其他为绿

 

给textView定义一个id,定义id的方法:android:id="@+id/tv"

 

android:autoLink="all" 表示web/mail/map之类的都加链接

 

修改main.xml:

<TextView 
	    android:id="@+id/tv"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:textSize="20sp"
	    android:textColor="#00ff00"
	    android:text="@string/say"
	    />

 

 String.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, TextVitwDemoActivity!</string>
    <string name="say">Hello World, 欢迎观看《android教程》哈哈!</string>
    <string name="app_name">TextViewDemo</string>

</resources>

 

TextVitwDemoActivity.java:

package com.mhm.textView;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class TextVitwDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.tv);
        String text = tv.getText().toString();
        int tb = text.indexOf("《");
        int te = text.indexOf("》") + 1;
        String text_b = text.substring(0, tb);
        String text_m = text.substring(tb, te);
        String text_e = text.substring(te, text.length());
        tv.setText(Html.fromHtml(text_b + "<font color=red>" + text_m + "</font>" + text_e));
    }
}

 

 

 

 

也可以这样写:

package com.mhm.textView;

import android.R.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class TextVitwDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.tv);
        String text = tv.getText().toString();
        int tb = text.indexOf("《");
        int te = text.indexOf("》") + 1;
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.GREEN), 0, tb, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.RED), tb, te, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.GREEN), te, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
    }
}

 这里有个奇怪的地方,int te = text.indexOf("》") + 1

+1必须写在这里,写在下面就不行。。。啥原因?

 

 

 

 

  • 大小: 13.5 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值