Android-在文本视图中突出显示一个单词?
我有一个Android-在文本视图中突出显示一个单词?,android,android-listview,highlighting,textview,Android,Android Listview,Highlighting,Textview,我有一个数据库搜索查询,它在数据库中搜索用户输入的单词,并返回一个光标 在我的ListActivity中,我有一个ListView,它将保存项目(光标项目)。ListView项目布局基本上是一个TextView。我的意思是,ListView将是TextView的列表 我想突出显示搜索词,无论它出现在文本视图中的哪个位置。我的意思是突出显示:不同的颜色或不同的背景色或任何东西使它不同于文本的其余部分 这可能吗?怎么做? 更新: cursor = myDbHelper.search(term);
数据库搜索查询
,它在数据库中搜索用户输入的单词,并返回一个光标
在我的ListActivity
中,我有一个ListView
,它将保存项目(光标项目)。ListView
项目布局基本上是一个TextView
。我的意思是,ListView
将是TextView
的列表
我想突出显示搜索词
,无论它出现在文本视图
中的哪个位置。我的意思是突出显示:不同的颜色或不同的背景色或任何东西使它不同于文本的其余部分
这可能吗?怎么做?
更新:
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()
方法将返回一些标题。我想突出显示那些标题中与术语
单词匹配的单词。我希望这一点现在已经清楚了。我还没有做到,但这看起来很有希望:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/guide/topics/resources/string-resource.html
公共最终void setText(字符序列文本)
因为:API级别1设置TextView的字符串值。文本框
不接受类似HTML的格式设置,您可以使用文本进行设置
XML资源文件中的字符串。要设置字符串的样式,请附加
android.text.style.*对象,或查看
有关设置示例的可用资源类型文档
XML资源文件中的格式化文本
在word周围插入颜色的HTML代码,并将其设置为textView 像
String newString=oldString.replaceAll(textToHighlight,“+textToHighlight+”);
setText(Html.fromHtml(newString));
数字2和4是文本着色的开始/停止索引,在本例中,“rti”将被着色
因此,您基本上只需在标题中找到搜索词的起始索引:
int startIndex = titleText.indexOf(term);
int stopIndex = startIndex + term.length();
然后用索引替换数字2和4,用标题字符串替换“部分彩色文本”
来源:尝试此库
启动位置
TextView.setText()
将参数获取为Spannable
而不仅仅是CharacterSequence
。SpannableString有一个方法setSpan()
,允许应用样式
请参见直接子类表单CharacterStyle列表
- 在“Hello,World”中为单词“Hello”指定背景色和前景色的示例
Spannable=new SpannableString(“你好,世界”);
//设置红色前景色
ForegroundSpan fgSpan=新的ForegroundColorSpan(颜色为红色);
//设置蓝色背景色
BackgroundSpan bgSpan=新的BackgroundColorSPan(Color.blue);
//设置范围需要开始和结束索引
//在我们的例子中,是0和5
//但是,您可以直接设置fgSpan或bgSpan,
//要重用定义的CharacterStyle,请使用CharacterStyle.wrap()
spannable.setSpan(CharacterStyle.wrap(fgSpan),0,5,0);
spannable.setSpan(CharacterStyle.wrap(bgSpan),0,5,0);
//在文本视图上应用spannableString
textView.setText(spannable);
我知道这是一个老问题,但我已经创建了一个方法来突出显示字符串\段落中重复的单词
private Spannable highlight(int color, Spannable original, String word) {
String normalized = Normalizer.normalize(original, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
int start = normalized.indexOf(word);
if (start < 0) {
return original;
} else {
Spannable highlighted = new SpannableString(original);
while (start >= 0) {
int spanStart = Math.min(start, original.length());
int spanEnd = Math.min(start+word.length(), original.length());
highlighted.setSpan(new ForegroundColorSpan(color), spanStart,
spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
start = normalizedText.indexOf(word, spanEnd);
}
return highlighted;
}
}
如果字符串是静态的,则在
xml字符串中执行此操作
<string name="my_text">This text is <font color='red'>red here</font></string>
此文本在此处为红色
更简单的方法
可以使用类突出显示/格式化部分文本
textView.setText("Hello, I am Awesome, Most Awesome"); // set text first
setHighLightedText(textView, "a"); // highlight all `a` in TextView
这里是方法
/**
* use this method to highlight a text in TextView
*
* @param tv TextView or Edittext or Button (or derived from TextView)
* @param textToHighlight Text to highlight
*/
public void setHighLightedText(TextView tv, String textToHighlight) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
// set color here
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}
/**
*使用此方法在TextView中高亮显示文本
*
*@param tv TextView或Edittext或按钮(或从TextView派生)
*@param textToHighlight要突出显示的文本
*/
public void setHighLightedText(text查看电视,字符串text突出显示){
字符串tvt=tv.getText().toString();
intofe=tvt.indexOf(textToHighlight,0);
Spannable wordToSpan=新的SpannableString(tv.getText());
对于(intofs=0;ofs
你可以点击突出显示的文本。我知道这个线程很旧,但为了防止有人在文本视图中突出显示字符串,我创建了一个库,可以完全做到这一点。这是我对堆栈溢出问题的第一个回答,因为我刚刚加入,希望它的格式正确且相关。它使用SpannableString并将查找您指定的字符串的所有匹配项。此外,内置了一个自定义ClickableSpan,可以根据需要为单击的文本设置侦听器
连接器
轻量级android库,用于突出显示textview中的字符串(忽略大小写),并带有可选回调
语言:Java
明斯克:17
可以找到它的功能和所有代码的图像
要在android项目中实现工件,请执行以下操作:
在项目级build.gradle中
所有项目{
存储库{
...
maven{url'https://jitpack.io' }
}
}
在应用程序级别build.gradle中
依赖项{
实现'com.github.Gaineyj0349:Linker:1.2'
}
如何使用:
1-使用textview构造链接器对象:
Linker Linker=新链接器(textView);
2-添加要在textview文本中突出显示的数组或字符串列表:
ArrayList list=new ArrayList();
添加(“你好”);
列表。添加(“世界”);
linker.addStrings(列表);
和/或
String[]words=新字符串[]{“一”、“二”、“三”};
linker.addString
<string name="my_text">This text is <font color='red'>red here</font></string>
textView.setText("Hello, I am Awesome, Most Awesome"); // set text first
setHighLightedText(textView, "a"); // highlight all `a` in TextView
/**
* use this method to highlight a text in TextView
*
* @param tv TextView or Edittext or Button (or derived from TextView)
* @param textToHighlight Text to highlight
*/
public void setHighLightedText(TextView tv, String textToHighlight) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
// set color here
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}
private void highlightMask(TextView textView, String text, String mask) {
boolean highlightenabled = true;
boolean isHighlighted = false;
if (highlightenabled) {
if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(mask)) {
String textLC = text.toLowerCase();
mask = mask.toLowerCase();
if (textLC.contains(mask)) {
int ofe = textLC.indexOf(mask, 0);
Spannable wordToSpan = new SpannableString(text);
for (int ofs = 0; ofs < textLC.length() && ofe != -1; ofs = ofe + 1) {
ofe = textLC.indexOf(mask, ofs);
if (ofe == -1) {
break;
} else {
// set color here
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + mask.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordToSpan, TextView.BufferType.SPANNABLE);
isHighlighted = true;
}
}
}
}
}
if (!isHighlighted) {
textView.setText(text);
}
}