Android搜索关键字变色
16lz
2021-01-23
/** * 关键字高亮变色 * * @param color 变化的色值 * @param text 文字 * @param keyword 文字中的关键字 * @return */ public static SpannableString findSearch(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 findSearch(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; }
更多相关文章
- android中TextView的文字实现动态效果,走马灯效果,闪烁效果
- 【Java】java和android网络编程 - 对byte数组压缩和解压缩(zip,g
- Android文字垂直滚动、纵向走马灯的几种实现方式
- android 修改标题栏文字居中
- Android搜索TextView显示关键字标红(忽略大小写)
- android 设置中文字体。。。。
- android 线程 synchronized关键字
- android之xml定义数组
- Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]