Should be a long time did not write about the technical class article, the day before yesterday there are people in the group asked me, said the group of the Lord for a long period did not share dry, today to share an Android TextView in a large section of the text content how to make the key word color of the article, I hope to help you, I finally got back on the Crooked Road. This article in peacetime should be more commonly used, if you will, you do not have to see, if not, you can take a look, very simple.
Today's share of the article about how to make a large paragraph in the text content of the keyword color highlighting, divided into a keyword highlighting color display and a number of keywords highlighted color display. TextView I have encapsulated it into the Keywordutil tool class, which can be called directly, as follows:
The specific code is as follows:
Package net.loonggg.test;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import android.text.SpannableString;
Import android.text.Spanned;
Import Android.text.style.ForegroundColorSpan;
public class Keywordutil {
/**
* Keywords highlighting color
*
* @param color
* Change in color value
* @param text
* Text
* @param keyword
* Keywords in the text
* @return
*/
public static spannablestring matchersearchtitle (int color, String text,
String keyword) {
spannablestring s = new spannablestring (text);
Pattern p = pattern.compile (keyword);
Matcher m = P.matcher (s);
while (M.find ()) {
int start = M.start ();
int end = M.end ();
S.setspan (new Foregroundcolorspan (color), start, end,
spanned.span_exclusive_exclusive);
}
return s;
}
/**
* Multiple keywords highlighting color
*
* @param color
* Change in color value
* @param text
* Text
* @param keyword
* Key word groups in text
* @return
*/
public static spannablestring matchersearchtitle (int color, String text,
string[] keyword) {
spannablestring s = new spannablestring (text);
for (int i = 0; i < keyword.length; i++) {
Pattern p = pattern.compile (Keyword[i]);
Matcher m = P.matcher (s);
while (M.find ()) {
int start = M.start ();
int end = M.end ();
S.setspan (new Foregroundcolorspan (color), start, end,
spanned.span_exclusive_exclusive);
}
}
return s;
}
}
In fact, after reading the code will know, very simple. You can try it!
Highlight color in Android display keywords in text