package com.example.fortextdemo; 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(); } } package com.example.fortextdemo; 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); } }
源码下载地址:http://www.apkbus.com/android-245433-1-1.html
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦