Determination of illegal characters of Android and determination of emoji

Source: Internet
Author: User

public class Emojiedittext extends EditText {
Enter the cursor position before the expression
private int cursorpos; Enter the text in the EditText before the emoticon
Private String Inputaftertext; Whether to reset the contents of the EditText
Private Boolean Resettext;
Private Context Mcontext;


Public Emojiedittext (Context context) {
Super (context);
This.mcontext = context;
Initedittext ();
}


Public Emojiedittext (context context, AttributeSet Attrs) {
Super (context, attrs);
This.mcontext = context;
Initedittext ();
}


Public Emojiedittext (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 in memory point to the same address, s changed,
Inputaftertext changed, so 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;
Input emoji emoji is not supported at this stage
Toast.maketext (mcontext, "input emoji not supported",
Toast.length_short). Show (); Is the emoji to revert the text to the content before the input emoji
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 does not match, the character is a emoji expression
return true;
}
}
return false;
}


/**
* determine if emoji
*&NBSP;
* @param codepoint
*             Comparison single character
* @return
*/
private Static Boolean Isemojicharacter (char codepoint) {
return (codepoint = 0x0) | | (codepoint = = 0x9) | | (codepoint = = 0xA)
| | (codepoint = = 0xD)
| | ((codepoint >= 0x20) && (codepoint <= 0xd7ff))
| | ((codepoint >= 0xe000) && (codepoint <= 0xFFFD))
| | ((codepoint >= 0x10000) && (codepoint <= 0x10ffff));
}


/span>

}

Determination of illegal characters of Android and determination of 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.