Sending, displaying, pasting, and deleting emoticon

Source: Internet
Author: User

Sending, displaying, pasting, and deleting emoticon

Recently, I am also working on the insert expression function. There are a lot of information about the use of the expression on the Internet, but most of the information is lacking in arms and legs. After referring to some examples on the internet, I made a complete demo to share with you. Of course, this demo also has some optimization problems. If you are interested, continue to optimize it. I have made the project source code open source on my github, https://github.com/arjinmc/FaceDemo

Many examples on the Internet store our emoticon key-value pairs in the form of arrays, but this mode is not used in actual projects, because this mode has poor scalability. As a result, I used the file mode for access, which is still similar to the corresponding relationship for access: [emoticons] = image name.

This function has two important controls that must be rewritten. If this parameter is not overwritten, you need to write a lot of code to rewrite TextView and EditText.

In TextView, we mainly rewrite setText ()

 

@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 used to display emoticon. This method also needs to be rewritten in EditText, and the code is similar. In addition to the first time, you also need to rewrite an action listener for text pasting, so that you can paste an 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 also the deletion of emotices. In many big-name applications, the deletion button is usually placed in the emoticon heap. This deletion button can also act as the deletion key of the soft keyboard, that is, you can delete a single character or a single expression. This is what I wrote.

 

/*** @ Usage Delete the string or expression on editetext * @ author eminem * @ email eminem@hicsg.com * @ 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 = tempS Tring. 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 );}}

 

 

Another key point is how to determine whether the emoticon is selected or the delete key is clicked. I will define a constant for the deletion key to indicate that this is the deletion. When the click emoticons event is triggered, I use handler to pass the message, when the interface receives this message, it is processed accordingly.

 

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.