Android - 在 TextView 中突出显示一个单词? [英] Android - Highlight a Word In a TextView?
问题描述
我有一个数据库搜索查询
,它在数据库中搜索用户输入的单词并返回一个Cursor
.
I have a database search query
which search in the database for a word entered by the user and return a Cursor
.
在我的 ListActivity
中,我有一个 ListView
将保存项目(光标项目).ListView
项目布局基本上是一个TextView
.我的意思是,ListView
将是 TextView
的列表.
In my ListActivity
, I have a ListView
which will hold the items (the Cursor items). The ListView
items layout is basically a TextView
. I mean, the ListView
will be a list of TextView
's.
我想要的是在 TextView
中的任何位置突出显示 search term
.我的意思是突出显示:不同的颜色或不同的背景颜色或任何使它与文本的其余部分不同的东西.
What I want is to highlight the search term
wherever it appears in the TextView
. I mean by highlighting: different color or different background color or anything makes it different than the rest of the text.
这可能吗?以及如何?
更新:
cursor = myDbHelper.search(term); //term: a word entered by the user.
cursor.moveToFirst();
String[] columns = {cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.item_title}; //item_title: the TextView holding the one raw
ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
lv = getListView();
lv.setAdapter(ca);
对于@Shailendra:search()
方法将返回一些标题.我想突出显示与 term
单词匹配的标题中的单词.我希望这现在很清楚了.
For @Shailendra: The search()
method will return some titles. I want to highlight the words in those titles that matches the term
word. I hope this is clear now.
推荐答案
在 word 周围插入颜色的 HTML 代码并将其设置为您的 textView .
insert HTML code for color around word and set it to your textView .
喜欢
String newString = oldString.replaceAll(textToHighlight, "<font color='red'>"+textToHighlight+"</font>");
textView.setText(Html.fromHtml(newString));
这篇关于Android - 在 TextView 中突出显示一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!