TextView高亮显示

TextView高亮显示

TextView的高亮显示主要用到的是SpannableStringBuilder类的setSpan()方法,这个方法有4个参数:

public voidsetSpan(Objectwhat, intstart, intend, intflags) {

       

    }

what:高亮颜色。。。(ForegroundColorSpan,URLSpan,StyleSpan,StrikethroughSpan,UnderlineSpan,ImageSpan)

start:开始字符位置

end:结束字符位置

flags:类别(例子这里输入前后都不包括)

如:

spannable.setSpan(newForegroundColorSpan(Color.RED), 2, 4,

              SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);

 

代码展示:

package lyx.feng.other;

 

import lyx.feng.simpletextdemo.R;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.text.SpannableStringBuilder;

import android.text.style.ForegroundColorSpan;

import android.widget.TextView;

 

public class SpinnableActivity extends Activity {

    private TextView tv = null;

    private TextView tv02 = null;

    private String info = "这是高亮显示的文字!";

    private String info02 = "这是默认显示的文字!";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.spinnable);

       this.tv = (TextView) super.findViewById(R.id.spinnable_tv);

       this.tv02 = (TextView) super.findViewById(R.id.spinnable_tv02);

       SpannableStringBuilder spannable = new SpannableStringBuilder(info);

       spannable.setSpan(new ForegroundColorSpan(Color.RED), 2, 4,

              SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);

 

       this.tv.setText(spannable);

 

        this.tv02.setText(info02);

       this.tv02.setTextColor(Color.RED);

 

    }

}

 

效果:TextView高亮显示_第1张图片

你可能感兴趣的:(小项目,TextView高亮显示)