String fullText =“我们尊敬的总理”;
String searchText =“onor”;
我可以很好地突出显示“ onor ”文字。 [使用底码]
但是,当我的搜索字符串为“ onor ”时,我想强调完整的“ 尊敬的 ”文字。
我的代码是:
public static void setSearchText(TextView textView, String fullText, String searchText) {
// highlight search text
if (null != searchText && !searchText.isEmpty()) {
int startPos = fullText.indexOf(searchText);
int endPos = startPos + searchText.length();
if (startPos != -1) {
int ofe = fullText.indexOf(searchText, 0);
Spannable WordtoSpan = new SpannableString(fullText);
for (int ofs = 0; ofs < fullText.length() && ofe != -1; ofs = ofe + 1) {
ofe = fullText.indexOf(searchText, ofs);
if (ofe == -1)
break;
else {
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
WordtoSpan.setSpan(highlightSpan, ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new RelativeSizeSpan(1.5f), ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
}
} else {
textView.setText(fullText);
}
} else {
textView.setText(fullText);
}
}
答案 0 :(得分:1)
一旦您在全文中找到了搜索字符串的匹配项,您需要将该匹配项向外扩展到字边界(空格)。
这是一种方法:
//对于单项搜索
private static void setSearchText(TextView textView, final String fullText, final String searchText) {
// highlight search text
if (null != searchText && !searchText.isEmpty()) {
int startPos = fullText.indexOf(searchText);
int endPos = startPos + searchText.length();
if (startPos != -1) {
// Found a match to the partial text -- now search outward to
// the word boundaries
final char WORD_BOUNDARY = ' ';
final char WORD_BOUNDARY1 = '\n';
int wordStart = startPos;
while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY && fullText.charAt(wordStart) != WORD_BOUNDARY1) {
--wordStart;
}
wordStart = wordStart + 1;
int wordEnd = endPos;
while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY && fullText.charAt(wordEnd) != WORD_BOUNDARY1) {
++wordEnd;
}
// Now highlight based on the word boundaries
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
Spannable wordtoSpan = new SpannableString(fullText);
wordtoSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new RelativeSizeSpan(1.5f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordtoSpan, TextView.BufferType.SPANNABLE);
} else {
textView.setText(fullText);
}
} else {
textView.setText(fullText);
}
}
//对于多字符串搜索使用以下代码:
public static void setSearchText(TextView textView, final String fullText, final String searchText) {
// highlight search text
if (null != searchText && !searchText.isEmpty()) {
int startPos = fullText.indexOf(searchText, 0);
int endPos = startPos + searchText.length();
if (startPos != -1) {
// Found a match to the partial text -- now search outward to
// the word boundaries
Spannable wordtoSpan = new SpannableString(fullText);
for (int i = 0; i < fullText.length() && startPos != -1; i = startPos + 1) {
startPos = fullText.indexOf(searchText, i);
endPos = startPos + searchText.length();
if (startPos == -1)
break;
else {
final char WORD_BOUNDARY = ' ';
final char WORD_BOUNDARY1 = '\n';
int wordStart = startPos;
while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY && fullText.charAt(wordStart) != WORD_BOUNDARY1) {
--wordStart;
}
wordStart = wordStart + 1;
int wordEnd = endPos;
while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY && fullText.charAt(wordEnd) != WORD_BOUNDARY1) {
++wordEnd;
}
// Now highlight based on the word boundaries
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
wordtoSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new RelativeSizeSpan(1.5f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordtoSpan, TextView.BufferType.SPANNABLE);
}
}
} else {
textView.setText(fullText);
}
} else {
textView.setText(fullText);
}
}