Add edittext to image mixing display

Source: Internet
Author: User

Android webview although powerful, however, it often depends on Android components. The general usage of these components is unnecessary here, and there are many interesting usage methods in these components. For example, developers who have used QQ for Android may find that when entering chat records, you can directly put the emoticon image in edittext component ( 3 ). Many beginners may feel that it is difficult to implement the function , but it is actually easier to implement this function.


We generally useEdittextOfSettextMethodSetEdittextFor exampleCodeDirectionEdittextAdded a string.

Edittext=(Edittext) findbyview (R. Id. edittext );
Edittext. settext ("TestData");

We only need to do a little bit of the above Code to insert the imageEdittext. If you have read 《Android/ophoneDevelop a complete handout, you will find that the book uses text markup in many cases (similarHtmlLanguage labels, but only contain very limitedHtmlLanguage functions ). There isMark. With this tag, you can easily insert an imageEdittext. HoweverA tag has one more step than other labels.ImagegetterInterface to obtainDrawableObject. Let's take a look at the following code:

  1. Imagegetter = new imagegetter ()
  2. {
  3. @ Override
  4. Public drawable getdrawable (string source)
  5. {
  6. Int id = integer. parseint (source );
  7. Drawable d = getresources (). getdrawable (ID );
  8. D
  9. . Setbounds (0, 0, D. getintrinsicwidth (), d
  10. . Getintrinsicheight ());
  11. Return D;
  12. }
  13. };

Copy code

The above code passes throughImagegetterInterfaceGetdrawableMethod returnsDrawableObject, whereSourceThe parameter value isLabelSRCAttribute Value. We need to use the following codeLabel.

Edittext.AppEnd (html. fromhtml ("

+ Faces [new random (). nextint (6)] + "'/>", imagegetter, null ));

Where Faces Is Int Type array, saving the corresponding Drawable ResourcesOf IDValue. In use Use Html. fromhtml Method To encapsulate these labels. Label. Fromhtml Method 2 Parameter (s) specified Imagegetter Object Label SRC Attribute to obtain the corresponding Drawable Object. Pass Edittext. append Insert Method The system willAutomaticSet The label is converted to the corresponding image and displayed in Edittext . Let's take a look at our results ( 4 ).

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.