最優(yōu)秀的無錫網(wǎng)站建設(shè),推廣賺錢的app,免費婚慶網(wǎng)站模板,采購信息網(wǎng)效果 第一步:按如下所示代碼創(chuàng)建一個用來高亮顯示文本的工具類:
public class KeywordUtil {/*** 單個關(guān)鍵字高亮變色* param color 變化的色值* param text 文字* param keyword 文字中的關(guān)鍵字* return*/public static SpannableString highLigh…
效果
第一步:按如下所示代碼創(chuàng)建一個用來高亮顯示文本的工具類:
publicclassKeywordUtil{/*** 單個關(guān)鍵字高亮變色* @param color 變化的色值* @param text 文字* @param keyword 文字中的關(guān)鍵字* @return*/publicstaticSpannableStringhighLight(int color,String text,String keyword){SpannableString res =newSpannableString("\t\t\t"+ text);Pattern pattern =Pattern.compile(keyword);Matcher matcher = pattern.matcher(res);while(matcher.find()){int start = matcher.start();int end = matcher.end();res.setSpan(newForegroundColorSpan(color), start, end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}return res;}publicstaticSpannableStringhighLight(int color,String text,String... keyword){SpannableString res =newSpannableString("\t\t\t"+ text);for(String key : keyword){Pattern pattern =Pattern.compile(key);Matcher matcher = pattern.matcher(res);while(matcher.find()){int start = matcher.start();int end = matcher.end();res.setSpan(newForegroundColorSpan(color), start, end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}}return res;}publicstaticSpannableStringhighLight(int[] cs,String text,String... keyword){SpannableString res =newSpannableString("\t\t\t"+ text);for(int i =0; i < color.length; i++){Pattern pattern =Pattern.compile(keyword[i]);Matcher matcher = pattern.matcher(res);while(matcher.find()){int start = matcher.start();int end = matcher.end();res.setSpan(newForegroundColorSpan(cs[i]), start, end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}}return res;}}