Android inserts emoticon images into edittext

Source: Internet
Author: User

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

Related Article

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.