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 ;}