Highlight color in Android display keywords in text

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.