Send, show, paste and delete emoticons

Source: Internet
Author: User

Recently I also do insert expression function, online has a lot of information about the use of expressions, but most of the information is missing arms and legs. Referring to some examples on the internet, I did a more perfect demo share to everyone, of course, this demo also has some optimization problems, interested people continue to optimize it. I've got the project source code open on my GitHub, Https://github.com/arjinmc/FaceDemo.

There are many examples on the web that store our expression key pairs in the form of arrays, but this pattern is not used in real-world projects because of the poor extensibility of this pattern. So I use the file mode to access, is still similar to the corresponding relationship to access: [expression character]=== image name.

There are two important controls that you must override to do this function. If you do not rewrite, you need to write a lot of code, respectively, TextView and EditText rewrite.

Main rewrite SetText () in TextView

@Override   publicvoidsettext (charsequence text, Buffertype type) {       spannablestring spannablestring = new Spannablestring (text);       spannablestring = faceutil.getexpressionstring (GetContext (), spannablestring.tostring ());        Super.settext (spannablestring, type);}

This is the main code to display the expression, also in the edittext need to rewrite this method, the code is similar. For the first time, you also need to rewrite a text to paste the action monitoring, so you can paste the expression.

@Override   publicbooleanontextcontextmenuitem (int id) {          if (id = = Android). R.id.paste) {               clipboardmanagerclipboard= (Clipboardmanager) GetContext (). Getsystemservice (Context.CLIPBOARD_ SERVICE);                String value = Clipboard.gettext (). toString ();                Editable edit = Geteditabletext ();                int cursorselect = Getselectionstart ();                Edit.insert (Cursorselect, Faceutil.getexpressionstring (GetContext (), value));                return true;          }          return Super.ontextcontextmenuitem (ID);}

There is to delete the expression, in many big-names applications, delete the expression button is usually placed in the expression heap inside, this delete button can also act as a soft keyboard delete key, that is, you can delete a single character can also delete a single expression. That's what I wrote.

/**     * @usage Delete the string or expression on Editetext *     @author Eminem     * @email [email protected]     * @website arjinmc.com     * @create December 26, 2014     *    /private void deleonestring () {       int edittextcursor = Edittext.getselectionstart ();       Editableeditable= Edittext.gettext ();        Stringtempstring= Edittext.gettext (). toString (). substring (0,edittextcursor);       int end = Tempstring.lastindexof ("]");       if (end==edittextcursor-1) {           int start = Tempstring.lastindexof ("[");           if (start!=-1) {              editable.delete (start, edittextcursor);           } else if (edittextcursor!=0) {              editable.delete (edittextCursor-1, edittextcursor);           }       } else if (edittextcursor!=0) {           editable.delete (edittextCursor-1, edittextcursor);       }    }

There is also a focus, how to determine whether to choose the expression or click the Delete button. I will define a constant for the delete key to indicate that this is the deletion, when the click on the expression of the event triggered, I used the handler to pass the message, when the interface received the message, then the corresponding processing. The code is complete on GitHub, and it's interesting to download to see.

[email protected] This mailbox will continue to use, casually send me which. Welcome harassment.

Send, show, paste and delete emoticons

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.