Android 高亮关键字TextView

作者: MrTrying | 来源:发表于2018-06-09 14:36 被阅读85次

    最近有需求做一个标记关键字的文本功能,仅此记录分享一下

    需求很简单,就是标记搜索的关键字,这里是需要标记每一个字,具体想过如下:

    效果图

    其实思路比较简单,对文本中的文字进行匹配,匹配后使用SpannableString(或者SpannableStringBuilder)修改文本的颜色。主要代码如下:

    public SpannableStringBuilder matcherSignText() {
        if (TextUtils.isEmpty(mOriginalText)) {
            return new SpannableStringBuilder("");
        }
        if (TextUtils.isEmpty(mSignText)) {
            return new SpannableStringBuilder(mOriginalText);
        }
        //关键代码
        SpannableStringBuilder builder = new SpannableStringBuilder(mOriginalText);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(mSignTextColor);
        for (int index = 0; index < mSignText.length(); index++) {
            final char c = mSignText.charAt(index);
            Pattern p = Pattern.compile(String.valueOf(c));
            Matcher m = p.matcher(mOriginalText);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                builder.setSpan(foregroundColorSpan, start, end, 
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return builder;
    }
    

    为了方便使用,为关键字和关键字颜色定义了自定义属性

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="SignKeyWordTextView">
            <attr name="signText" format="string"/>
            <attr name="signTextColor" format="color"/>
        </declare-styleable>
        
    </resources>
    

    在xml中使用相对比较方便

    <com.mrtrying.SignKeyWordTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"
        android:textColor="#c4c4c4"
        android:textSize="@dimen/sp_15"
        app:signTextColor="#FFDA44"/>
    

    最后上源码

    package com.mrtrying;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.support.v7.widget.AppCompatTextView;
    import android.text.SpannableStringBuilder;
    import android.text.Spanned;
    import android.text.TextUtils;
    import android.text.style.ForegroundColorSpan;
    import android.util.AttributeSet;
    
    import com.quze.lbsvideo.R;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * Description :
     * PackageName : com.mrtrying
     * Created by mrtrying on 2018/6/7 10:24.
     * e_mail : ztanzeyu@gmail.com
     */
    public class SignKeyWordTextView extends AppCompatTextView {
    
        //原始文本
        private String mOriginalText = "";
        //关键字
        private String mSignText;
        //关键字颜色 
        private int mSignTextColor;
    
        public SignKeyWordTextView(Context context) {
            super(context);
        }
    
        public SignKeyWordTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initializeAttrs(attrs);
        }
    
        //初始化自定义属性
        private void initializeAttrs(AttributeSet attrs) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SignKeyWordTextView);
            //获取关键字
            mSignText = typedArray.getString(R.styleable.SignKeyWordTextView_signText);
            //获取关键字颜色
            mSignTextColor = typedArray.getColor(R.styleable.SignKeyWordTextView_signTextColor, getTextColors().getDefaultColor());
            typedArray.recycle();
        }
    
        //重写setText方法
        @Override
        public void setText(CharSequence text, BufferType type) {
            this.mOriginalText = text.toString();
            super.setText(matcherSignText(), type);
        }
    
        /**
         * 匹配关键字,并返回SpannableStringBuilder对象
         * @return
         */
        private SpannableStringBuilder matcherSignText() {
            if (TextUtils.isEmpty(mOriginalText)) {
                return new SpannableStringBuilder("");
            }
            if (TextUtils.isEmpty(mSignText)) {
                return new SpannableStringBuilder(mOriginalText);
            }
            SpannableStringBuilder builder = new SpannableStringBuilder(mOriginalText);
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(mSignTextColor);
            for (int index = 0; index < mSignText.length(); index++) {
                final char c = mSignText.charAt(index);
                Pattern p = Pattern.compile(String.valueOf(c));
                Matcher m = p.matcher(mOriginalText);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    builder.setSpan(foregroundColorSpan, start, end,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            return builder;
        }
    
        /**
         * 设置关键字
         * @param signText 关键字
         */
        public void setSignText(String signText) {
            mSignText = signText;
            setText(mOriginalText);
        }
    
        /**
         * 设置关键字颜色
         * @param signTextColor 关键字颜色
         */
        public void setSignTextColor(int signTextColor) {
            mSignTextColor = signTextColor;
            setText(mOriginalText);
        }
    
    }
    

    相关文章

      网友评论

        本文标题:Android 高亮关键字TextView

        本文链接:https://www.haomeiwen.com/subject/gvdteftx.html