Edittext 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, such as 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:
Java code
Drawable = getresources (). getdrawable (ID); // the image to be accessed
Drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight ());
// The text to be processed. [Smile] is the text to be replaced.
Spannablestring spannable = new spannablestring (gettext (). tostring () + "[Smile]");
// To replace the specified text with an image, use imagespan.
Imagespan span = new imagespan (drawable, imagespan. align_baseline );
// Start replacement. Note that the 2nd and 3rd parameters indicate where to start replacement and where to end replacement (start and end)
// The last parameter is similar to a set in mathematics. [5, 12) indicates that the value ranges from 5 to 12, including 5 but not 12.
Spannable. setspan (span, gettext (). Length (), gettext (). Length () + "[Smile]". Length (), spannable. span_inclusive_exclusive );
Settext (spannable );