Reprint Address: http://blog.csdn.net/elsdnwn/article/details/45390771
Package com.liujy.ui.wiget;import android.content.context;import android.text.editable;import android.text.selection;import android.text.spannable;import android.text.textwatcher;import android.util.AttributeSet;import android.widget.EditText;public class containsemojiedittext extends edittext { //the cursor position before entering the emoticon private int cursorpos; //input EditText text in front of emoticons private string inputaftertext; //Reset the contents of EditText private boolean resettext; private context mcontext; public containsemojiedittext (Context context) { super (context); this.mcontext = context; Initedittext (); } public containsemojiedittext (Context context, attributeset attrs) { super ( Context, attrs); this.mcontext = context; initedittext (); } Public containsemojiedittext (context context, attributeset attrs, int defstyleattr) { super (context, attrs, DEFSTYLEATTR); this.mcontext = context; initedittext (); } // Initialize the edittext control private void initedittext () { addtextchangedlistener (nEw textwatcher () { @Override public void Beforetextchanged (Charsequence s, int start, int before, int count) { if (! Resettext) { cursorpos = getselectionend (); // here with s.tostring () And not directly with s because if you use s, // So, Inputaftertext and s are pointing to the same address in memory, S has changed, &nbsP; // inputaftertext also changed, then the expression filter failed inputaftertext= s.tostring (); } } @Override public void ontextchanged (charsequence s, int start, int Before, int count) { if (!resettext) { if (count >= 2) {// Emoji have a minimum character length of 2 charsequence input = s.subsequence (Cursorpos, cursorpos + count); if (Containsemoji (input.tostring)) { resetText = true; toast.maketext (mcontext, "does not support input emoji emoji", toast.length_short). Show (); //is the emoji to revert text to the input emoji before the content SetText (Inputaftertext); charsequence text = gettext (); if (text instanceof spannable) { Spannable spanText = (spannable) text; selection.setselection (Spantext, text.length ()); } } } } else { resetText = false; } } @ override public void Aftertextchanged (editable editable) { } }); } /** * detect if there is emoji expression * * @param source * @return */ public static boolean containsemoji (String source) { int len = source.length (); for (int i = 0; i < len; i++) { char codepoint = source.charat (i); if (!isemojicharacter (codepoint)) { //If it doesn't match, Then the character is emoji emoticons return true; } } return false; } /** * determine if it is emoji * * @param codePoint Comparison of single characters * @return */ private static boolean isemojicharacter (Char codepoint) { return (codepoint == 0x0) | | (codepoint == 0x9) | | (codepoint == 0xa) | | (codepoint &NBSP;==&NBSP;0XD) | | ((codepoint >= 0x20) && (codepoint <= 0xd7ff)) | | (codepoint >= 0xe000) && (CODEPOINT&NBSP;<=&NBSP;0XFFFD)) | | ((codepoint >= 0x10000) && (CODEPOINT&NBSP;<=&NBSP;0X10FFFF)); }}
"Android" Android input box EditText prohibit input emoji emoji