一个让TextView的文本中制定关键字高亮显示的工具类
来源:互联网 发布:软件人才需求量 编辑:程序博客网 时间:2024/05/02 01:27
一个让TextView的文本中制定关键字高亮显示的工具类
在项目开发中很多地方需要让文本中某些关键字高亮显示,这样来说写个工具类是不是很好呢,嘿嘿是的。
package com.example.textkeyword;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.text.SpannableString;import android.text.Spanned;import android.text.style.ForegroundColorSpan;public class KeyWordUtil {/** * @param color 关键字颜色 * @param text 文本 * @param keyword 关键字 * @return */public static SpannableString keyWordHighLighting(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 keyWordHighLighting(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;}}
0 0
- 一个让TextView的文本中制定关键字高亮显示的工具类
- Android中TextView的文本内容中指定关键字高亮显示
- jQuery高亮显示文本中重要的关键字
- 让TextView的文本显示一行
- 如何在一个TextView中显示不同颜色的文本
- 一个TextView 显示多种样式的文本
- 用RTF实现文本编辑器中对关键字的高亮显示(1)
- 用RTF实现文本编辑器中对关键字的高亮显示(2)
- textview中显示的文本无法对齐
- Android中TextView的富文本显示
- 设置TextView的关键字高亮
- TextView如何高亮显示文本
- TextView如何高亮显示文本
- TextView的制定
- android textview 关键字高亮显示
- Android TextView指定关键字高亮显示
- 可以高亮显示关键字并设置不同字体大小的自定义TextView
- 显示文本进度的TextView
- android adb logcat的经典用法
- Spring MVC框架及标签库
- 1016. 部分A+B (15)
- 字母统计 19
- DB2下载地址
- 一个让TextView的文本中制定关键字高亮显示的工具类
- 四个整数的加减乘除(递归或枚举)
- Codeforces #333 (Div. 1) A. The Two Routes(最短路)
- DMR技术白皮书
- CSS 选择器
- Java 实现单例(Singleton)模式
- spark架构设计&编程模型01
- Mysql创建存储过程输入sql语句结束时出错
- Segment Tree