Android textview/edittext uses characterstyle & spannablestring to process image display, Font Style, hyperlink, etc.

Source: Internet
Author: User

Http://www.cnblogs.com/playing/archive/2011/03/18/1988182.html

 

Edittext:

 It is usually used to display text, but sometimes it also needs to include some pictures in the text, such as QQ, you can use emoticon images, or text highlighting, etc, how can this be done in Android?
Remember that there is an android. Text package in Android, which provides powerful text processing functions.
To add images, use the spannablestring and imagespan classes:

Drawable = getresources (). getdrawable (ID); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); // The text to be processed. [Smile] is the text to be replaced by spannablestring spannable = new spannablestring (gettext (). tostring () + "[Smile]"); // you must use imagespan span = new imagespan (drawable, imagespan. align_baseline); // start to replace. Note that parameters 2nd and 3rd indicate where to replace and where to replace (start and end) // The last parameter is similar to a set in mathematics, [5, 12) indicates from 5 to 12, including 5 but not 12 spannable. setspan (span, gettext (). length (), gettext (). length () + "[Smile]". length (), spannable. span_inclusive_exclusive); settext (spannable );

Highlight the required text:

Public void highlight (INT start, int end) {spannablestringbuilder spannable = new spannablestringbuilder (gettext (). tostring (); // used for variable string foregroundcolorspan span = new foregroundcolorspan (color. red); spannable. setspan (span, start, end, spannable. span_exclusive_exclusive); settext (spannable );}

Underline:

 
Public void underline (INT start, int end) {spannablestringbuilder spannable = new spannablestringbuilder (gettext (). tostring (); characterstyle span = new underlinespan (); spannable. setspan (span, start, end, spannable. span_exclusive_exclusive); settext (spannable );}

Combined use:

Spannablestringbuilder spannable = new spannablestringbuilder (gettext (). tostring (); characterstyle span_1 = new stylespan (Android. graphics. typeface. italic); characterstyle span_2 = new foregroundcolorspan (color. red); spannable. setspan (span_1, start, end, spannable. span_exclusive_exclusive); spannable. setspan (span_2, start, end, spannable. span_exclusive_exclusive); settext (spannable );

Case: You can use this method to display two colors for strings with/n linefeeds.

/*** Strings with/n linefeeds can be displayed in two colors using this method * @ Param text * @ Param color1 * @ Param color2 * @ return */Public spannablestringbuilder highlight (string text, int color1, int color2, int fontsize) {spannablestringbuilder spannable = new spannablestringbuilder (text); // variable string characterstyle span_0 = NULL, span_1 = NULL, span_2; int end = text. indexof ("/N"); If (END =-1) {// if there is no line break, use the first color to display span_0 = new foregroundcolorspan (color1); spannable. setspan (span_0, 0, text. length (), spannable. span_exclusive_exclusive);} else {span_0 = new foregroundcolorspan (color1); span_1 = new foregroundcolorspan (color2); spannable. setspan (span_0, 0, end, spannable. span_exclusive_exclusive); spannable. setspan (span_1, end + 1, text. length (), spannable. span_exclusive_exclusive); span_2 = new absolutesizespan (fontsize); // font size spannable. setspan (span_2, end + 1, text. length (), spannable. span_exclusive_exclusive);} return spannable ;}

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.