当前位置: 首页 > news >正文

高亮变色显示文本中的关键字

效果

在这里插入图片描述

第一步:按如下所示代码创建一个用来高亮显示文本的工具类:

public class KeywordUtil {/*** 单个关键字高亮变色* @param color   变化的色值* @param text    文字* @param keyword 文字中的关键字* @return*/public static SpannableString highLight(int color, String text, String keyword) {SpannableString res = new SpannableString("\t\t\t" + text);Pattern pattern = Pattern.compile(keyword);Matcher matcher = pattern.matcher(res);while (matcher.find()) {int start = matcher.start();int end = matcher.end();res.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}return res;}public static SpannableString highLight(int color,String text,String... keyword) {SpannableString res = new SpannableString("\t\t\t" + text);for (String key : keyword) {Pattern pattern = Pattern.compile(key);Matcher matcher = pattern.matcher(res);while (matcher.find()) {int start = matcher.start();int end = matcher.end();res.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}}return res;}public static SpannableString highLight(int[] cs,String text,String... keyword) {SpannableString res = new SpannableString("\t\t\t" + text);for (int i = 0; i < color.length; i++) {Pattern pattern = Pattern.compile(keyword[i]);Matcher matcher = pattern.matcher(res);while (matcher.find()) {int start = matcher.start();int end = matcher.end();res.setSpan(new ForegroundColorSpan(cs[i]), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}}return res;}
}

第二步:在主布局文件中添加三个id值分别为textView1、textView3、textView3的TextView控件,然后修改MainActivity类的代码如下所示:

public class MainActivity extends AppCompatActivity {private android.widget.TextView textView1;private android.widget.TextView textView2;private android.widget.TextView textView3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.textView2 = (TextView) findViewById(R.id.textView2);this.textView1 = (TextView) findViewById(R.id.textView1);this.textView3 = (TextView) findViewById(R.id.textView3);String text = "随着棉花糖的出现,一种新……度,探讨处理权限请求的方法";SpannableString spanStr1 = KeywordUtil.highLight(Color.RED, text, "权限");textView1.setText(spanStr1);SpannableString spanStr2 = KeywordUtil.highLight(Color.RED, text, "棉花糖","权限","安卓");textView2.setText(spanStr2);SpannableString spanStr3 = KeywordUtil.highLight(new int[]{Color.RED,Color.GREEN,Color.BLUE}, text, "棉花糖","权限","安卓");textView3.setText(spanStr3);}
}
http://www.lryc.cn/news/485537.html

相关文章:

  • Javascript垃圾回收机制-运行机制(大厂内部培训版本)
  • 【jvm】一个空Object对象的占多大空间
  • 241114.学习日志——[CSDIY] [CS]数据结构与算法 [00]
  • The Planets: Earth -- 练习
  • linux逻辑卷练习
  • openai 论文Scaling Laws for Neural Language Models学习
  • __VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined
  • 基于PHP技术的校园站的设计与实现
  • JVM回收机制与算法
  • 24/11/14 算法笔记 GMM高斯混合模型
  • Linux下编译安装Nginx
  • 算力100问☞第4问:算力的构成元素有哪些?
  • 安装paddle
  • 飞凌嵌入式RK3576核心板已适配Android 14系统
  • SpringBoot+MyBatis+MySQL的Point实现范围查找
  • 【Apache Paimon】-- 1 -- Apache Paimon 是什么?
  • 解决VsCode无法跳转问题
  • 优化C++设计模式:用模板代替虚函数与多态机制
  • 浪浪云轻量服务器搭建vulfocus网络安全靶场
  • C++builder中的人工智能(23):在现代C++ Windows上轻松录制声音
  • 避免误差!Android 中正确计算时间差的方式
  • unity3d————Resources异步加载
  • YOLOv11改进,YOLOv11添加GnConv递归门控卷积,二次创新C3k2结构
  • 如何选择国产化CMS来建设政务网站?
  • C/C++语言基础--initializer_list表达式、tuple元组、pair对组简介
  • paddle表格识别数据制作
  • python selenium库的使用:通过兴趣点获取坐标
  • 如何优化Kafka消费者的性能
  • 机器学习 决策树
  • 效益登记册效益管理计划