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