"Android" Android input box EditText prohibit input emoji emoji

Source: Internet
Author: User

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;&LT;=&NBSP;0XFFFD))  | |   ((codepoint >= 0x10000)                  &&  (CODEPOINT&NBSP;&LT;=&NBSP;0X10FFFF));     }}


"Android" Android input box EditText prohibit input emoji emoji

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.