Android多颜色TextView功能实现

时间:2023-02-11 10:28:10

1、直接显示带有颜色标签的html字符串

textview.setText(Html.fromHtml(str)); //str为含有html标签的字符串

2、简单字符串变色功能

SpannableStringBuilder style = new SpannableStringBuilder(str);      //str要显示的字符串        
style.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //0,2表示前两个字符显示蓝色
style.setSpan(new ForegroundColorSpan(Color.BLACK), 2, str.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //2到结尾显示黑色
textview.setText(style);

3、实现多颜色、多大小、多点击的功能

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements ClickListener {
private TextView textView1;
private ArrayList<String> str_list;
private ArrayList<Integer> color_list;
private ArrayList<Float> text_size_list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setData();
}

private void initView() {
textView1 = (TextView) findViewById(R.id.textView1);

}

private void setData() {
str_list = new ArrayList<String>();
color_list = new ArrayList<Integer>();
text_size_list = new ArrayList<Float>();
str_list.add("今天");
str_list.add("天气");
str_list.add("还不错!");
color_list.add(Color.RED);
color_list.add(Color.GREEN);
color_list.add(Color.BLUE);
text_size_list.add(15f);
text_size_list.add(17f);
text_size_list.add(20f);
Util.setText(this, textView1, str_list, color_list, text_size_list,
this);
}

@Override
public void click(int position) {
Log.e("position", position + "");
Toast.makeText(this, str_list.get(position), Toast.LENGTH_SHORT).show();

}
}
public interface ClickListener {	public void click(int position);}
import java.util.ArrayList;import android.content.Context;import android.graphics.Color;import android.text.Spannable;import android.text.SpannableString;import android.text.Spanned;import android.text.TextPaint;import android.text.method.LinkMovementMethod;import android.text.style.AbsoluteSizeSpan;import android.text.style.ClickableSpan;import android.text.style.ForegroundColorSpan;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class Util {	public static void setText(Context context, TextView tv,			ArrayList<String> str, ArrayList<Integer> color,			ArrayList<Float> text_size_list, ClickListener clickListener) {		// 累加数组所有的字符串为一个字符串		StringBuffer long_str = new StringBuffer();		for (int i = 0; i < str.size(); i++) {			long_str.append(str.get(i));		}		SpannableString builder = new SpannableString(long_str.toString());		// 设置不同字符串的点击事件		for (int i = 0; i < str.size(); i++) {			int p = i;			int star = long_str.toString().indexOf(str.get(i));			int end = star + str.get(i).length();			builder.setSpan(new Clickable(clickListener, p), star, end,					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);		}		// 设置不同字符串的颜色		ArrayList<ForegroundColorSpan> foregroundColorSpans = new ArrayList<ForegroundColorSpan>();		for (int i = 0; i < color.size(); i++) {			foregroundColorSpans.add(new ForegroundColorSpan(color.get(i)));		}		for (int i = 0; i < str.size(); i++) {			int star = long_str.toString().indexOf(str.get(i));			int end = star + str.get(i).length();			builder.setSpan(foregroundColorSpans.get(i), star, end,					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);		}		// 设置不同字符串的字号		ArrayList<AbsoluteSizeSpan> absoluteSizeSpans = new ArrayList<AbsoluteSizeSpan>();		for (int i = 0; i < color.size(); i++) {			absoluteSizeSpans.add(new AbsoluteSizeSpan(sp2px(context,					text_size_list.get(i))));		}		for (int i = 0; i < str.size(); i++) {			int star = long_str.toString().indexOf(str.get(i));			int end = star + str.get(i).length();			builder.setSpan(absoluteSizeSpans.get(i), star, end,					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);		}		// 设置点击后的颜色为透明,否则会一直出现高亮		tv.setHighlightColor(Color.TRANSPARENT);		tv.setClickable(true);		tv.setMovementMethod(LinkMovementMethod.getInstance());		tv.setText(builder);	}	public static int sp2px(Context context, float spValue) {		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;		return (int) (spValue * fontScale + 0.5f);	}}class Clickable extends ClickableSpan implements OnClickListener {	private final ClickListener clickListener;	private int position;	public Clickable(ClickListener clickListener, int position) {		this.clickListener = clickListener;		this.position = position;	}	@Override	public void onClick(View v) {		clickListener.click(position);	}	@Override	public void updateDrawState(TextPaint ds) {		// TODO Auto-generated method stub		super.updateDrawState(ds);		ds.setColor(Color.WHITE); // 设置文件颜色		ds.setUnderlineText(false);	}}