如何在字符串中搜索单词并在android的文本视图中突出显示单词? [英] How to search for a word in a string and highlight word in a text view in android?

查看:23
本文介绍了如何在字符串中搜索单词并在android的文本视图中突出显示单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 应用程序中,我有一个包含特定单词的字符串,所以我想在文本视图中显示整个字符串,并且应该突出显示特定的单词.希望下面的图片能给你一个想法.

in my android application i have a string that contains a specific word so i want to display whole string in text view and the specific word should be highlighted.Hope following image will give you an idea.

我已使用以下代码来执行此操作,但它不起作用.

I have used following code to do this but its not working.

代码:

con 是我的字符串,groupNameContent 是文本字段.

con is my string and groupNameContent is the text field.

con.replaceAll(arrGroupelements[groupPosition][5],"<font color='#CA278C'>"+arrGroupelements[groupPosition][5]+"</font>.");
groupNameContent.setText(Html.fromHtml(con));

推荐答案

对于每个单词,你可以使用:

for each word, you can use:

TextView textView = (TextView)findViewById(R.id.mytextview01);
//use a loop to change text color
Spannable WordtoSpan = new SpannableString("partial colored text");        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);

这篇关于如何在字符串中搜索单词并在android的文本视图中突出显示单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆