android TextView 部分文字变色 工具类
16lz
2021-01-23
import android.content.Context;import android.support.annotation.ColorRes;import android.support.annotation.NonNull;import android.support.annotation.StringRes;import android.text.Spannable;import android.text.SpannableString;import android.text.TextPaint;import android.text.method.LinkMovementMethod;import android.text.style.ClickableSpan;import android.util.Log;import android.view.View;import android.widget.TextView;/** * @Author: qqyang * @Date: 2019/12/27 * @Description: */public class ColorTextUtil { private static final String TAG = "ColorTextUtil"; private ColorTextUtil() { } /** * 设置 TextView 中部分文字颜色 (不设置 变色文字点击事件). */ public static void setColorText(@NonNull TextView tv, @StringRes int colorTextId, @ColorRes int colorId) { setColorText(tv, colorTextId, colorId, null); } /** * 设置 TextView 中部分文字颜色 (不设置 变色文字点击事件). */ public static void setColorText(@NonNull TextView tv, String colorTextStr, @ColorRes int colorId) { setColorText(tv, colorTextStr, colorId, null); } /** * 设置 TextView 中部分文字颜色. * * @param colorTextId 需要改变颜色的文字的 id。 * @param colorId 改变的颜色 * @param onColorTextClickListener 改变颜色文字的点击事件. */ public static void setColorText(@NonNull TextView tv, @StringRes int colorTextId, @ColorRes int colorId, OnColorTextClickListener onColorTextClickListener) { String allTextStr = tv.getText().toString().trim(); String colorTextStr = tv.getResources().getString(colorTextId); setColorTextClick(tv, allTextStr, colorTextStr, colorId, onColorTextClickListener); } /** * 设置 TextView 中部分文字颜色. * * @param colorText 需要改变颜色的文字 * @param colorId 改变的颜色 * @param onColorTextClickListener 改变颜色文字的点击事件. */ public static void setColorText(@NonNull TextView tv, String colorText, @ColorRes int colorId, OnColorTextClickListener onColorTextClickListener) { String allTextStr = tv.getText().toString().trim(); setColorTextClick(tv, allTextStr, colorText, colorId, onColorTextClickListener); } /** * 设置 TextView 中部分文字颜色. * * @param allTextStr 全部文字 * @param colorTextStr 需要改变颜色的文字 * @param colorId 改变的颜色 * @param onColorTextClickListener 改变颜色文字的点击事件. */ public static void setColorTextClick(TextView tv, String allTextStr, String colorTextStr, @ColorRes int colorId, OnColorTextClickListener onColorTextClickListener) { Log.i(TAG, "allTextStr: " + allTextStr); Log.i(TAG, "colorTextStr: " + colorTextStr); if (!allTextStr.contains(colorTextStr)) { Log.e(TAG, allTextStr + " 不包含 " + colorTextStr + " 文字!!!!"); return; } Context context = tv.getContext(); int start = allTextStr.indexOf(colorTextStr); int end = start + colorTextStr.length(); SpannableString spanText = new SpannableString(allTextStr); spanText.setSpan(new ClickableSpan() { @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(context.getResources().getColor(colorId)); //设置文字颜色 ds.setUnderlineText(false); } @Override public void onClick(View view) { if (null != onColorTextClickListener) { onColorTextClickListener.onColorTextClick(view); } } }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setHighlightColor(context.getResources().getColor(R.color.transparent));// 设置点击后的颜色为透明,否则会一直出现高亮 tv.setText(spanText); tv.setMovementMethod(LinkMovementMethod.getInstance());// 设置变色文字点击事件 } public static interface OnColorTextClickListener { void onColorTextClick(View view); }}
更多相关文章
- android监听edittext输入事件
- 毕设---android按钮事件
- Android Recyclerview 滚动事件监听
- Android ProgressBar 进度条颜色和背景颜色
- Android Tab点击监听 自定义事件
- android 复制、粘贴文字
- android 新手学习笔记 点击事件
- android gridview按钮边框和定制点击颜色
- 进度条及拖动条背景颜色设置(progressDrawable)