android 处理文字工具包括 高亮显示部分文字,部分文字可点击等

package com.mobile.mbank.launcher.utils;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpannableStringUtils {
    /**
     * 关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字
     * @return
     */

    public static SpannableString matcherSearchTitle(int color, String text, String keyword) {
        SpannableString s = new SpannableString(text);
        Pattern p = Pattern.compile(keyword);
        Matcher m = p.matcher(s);
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return s;
    }


    /**
     * 多个关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字数组
     * @return
     */

    public static SpannableString matcherSearchTitle(int color, String text, String[] keyword) {
        SpannableString s = new SpannableString(text);
        for (int i = 0; i < keyword.length; i++) {
            Pattern p = Pattern.compile(keyword[i]);
            Matcher m = p.matcher(s);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return s;
    }

    public static class MyClickSpan extends ClickableSpan {
        private int mHighLightColor = Color.BLUE;
        private boolean mUnderLine = false;
        private View.OnClickListener mClickListener;

        public MyClickSpan(View.OnClickListener listener) {
            this.mClickListener = listener;
        }

        public MyClickSpan(int color, boolean underline, View.OnClickListener listener) {
            this.mHighLightColor = color;
            this.mUnderLine = underline;
            this.mClickListener = listener;
        }

        public MyClickSpan(int mHighLightColor, View.OnClickListener mClickListener) {
            this.mHighLightColor = mHighLightColor;
            this.mClickListener = mClickListener;
        }

        @Override
        public void onClick(View widget) {
            if (mClickListener != null)
                mClickListener.onClick(widget);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(mHighLightColor);
            ds.setUnderlineText(mUnderLine);
        }

    }


    public static void setTextHighLightWithClick(TextView tv, String text, String keyWord, View.OnClickListener listener) {
        tv.setClickable(true);
        tv.setHighlightColor(Color.TRANSPARENT);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString s = new SpannableString(text);
        Pattern p = Pattern.compile(keyWord);
        Matcher m = p.matcher(s);
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new MyClickSpan(listener), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv.setText(s);
    }

    public static void setTextHighLightWithClick(TextView tv, String text, String keyWord, int color, View.OnClickListener listener) {
        tv.setClickable(true);
        tv.setHighlightColor(Color.TRANSPARENT);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString s = new SpannableString(text);
        Pattern p = Pattern.compile(keyWord);
        Matcher m = p.matcher(s);
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new MyClickSpan(color, listener), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv.setText(s);
    }


    /**
     * 多个关键字高亮变色 并且可点击
     *
     * @param tv
     * @param text      文字
     * @param keyword   文字中的关键字数组
     * @param colors
     * @param listeners
     */

    public static void setTextHighLightWithClicks(TextView tv, String text, String[] keyword, int[] colors, View.OnClickListener[] listeners) {
        tv.setClickable(true);
        tv.setHighlightColor(Color.TRANSPARENT);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString s = new SpannableString(text);

        for (int i = 0; i < keyword.length; i++) {
            Pattern p = Pattern.compile(keyword[i]);
            Matcher m = p.matcher(s);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                s.setSpan(new ForegroundColorSpan(colors[i]), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                s.setSpan(new MyClickSpan(colors[i], listeners[i]), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        tv.setText(s);
    }
}

你可能感兴趣的:(android 处理文字工具包括 高亮显示部分文字,部分文字可点击等)