Android 高亮關鍵字TextView
最近有需求做一個標記關鍵字的文字功能,僅此記錄分享一下
需求很簡單,就是標記搜尋的關鍵字,這裡是需要標記每一個字,具體效果如下:
其實思路比較簡單,對文字中的文字進行匹配,匹配後使用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);
}
}
相關文章
- HTML高亮關鍵字真麻煩HTML
- JavaScript 搜尋關鍵字高亮效果JavaScript
- JavaFx 關鍵字高亮文字實現Java
- javascript搜尋關鍵字高亮效果JavaScript
- javascript實現關鍵字高亮(轉)JavaScript
- Andriod給textview文字關鍵字迴圈標亮加粗TextView
- TextView搜尋文字高亮顯示TextView
- TextView 選中高亮動效 iOSTextViewiOS
- 將搜尋關鍵字設定為高亮顯示例項程式碼
- jquery搜尋關鍵詞高亮效果jQuery
- iOS中一種字串關鍵字檢索高亮的簡易實現iOS字串
- app直播原始碼,TextView部分字型顏色高亮APP原始碼TextView
- android--設定TextView部分文字的顏色和背景(高亮顯示)AndroidTextView
- 帝國CMS搜尋列表頁關鍵字高亮的更改實現方法教程
- vue2實現搜尋結果中的搜尋關鍵字高亮Vue
- 高亮:單關鍵詞、多關鍵詞、多組多關鍵詞,從簡單到複雜實現滿足多方面需求的頁面關鍵詞高亮
- abstract關鍵字 super 關鍵字 類與繼承繼承
- android textview設定字型的行距和字間距AndroidTextView
- 微信小程式實現搜尋關鍵詞高亮微信小程式
- Swift 關鍵字Swift
- typedef關鍵字
- Synchronized關鍵字synchronized
- JavaScript this關鍵字JavaScript
- Swift —— 關鍵字Swift
- oracle關鍵字Oracle
- Auto關鍵字
- ionic2/ionic3 實現搜尋結果中的搜尋關鍵字高亮
- 匹配搜尋關鍵高亮 new RegEXP 填坑
- 將搜尋關鍵詞高亮顯示例項程式碼
- MarklightObjC: 用於 TextView 的 Markdown 語法高亮,支援 iOS、macOSOBJTextViewiOSMac
- Google關鍵字工具Go
- Synchronize 關鍵字原理
- [java]transient關鍵字Java
- 使用 Synchronized 關鍵字synchronized
- c#關鍵字C#
- java中this關鍵字Java
- 多關鍵字排序排序
- JavaScript中this關鍵字JavaScript