Tool class:
Package com.example.test;
public class Emojifilter {
/**
* Detects if there are emoji characters
* @param source
* @return FALSE, including picture
*/
public static Boolean Containsemoji (String source) {
if (Source.equals ("")) {
return false;
}
int len = Source.length ();
for (int i = 0; i < len; i++) {
Char codepoint = Source.charat (i);
if (Isemojicharacter (codepoint)) {
Do nothing, judged here to indicate that there is an emoticon character confirmed
return true;
}
}
return false;
}
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));
}
/**
* Filter emoji or other non-text type characters
* @param source
* @return
*/
public static string Filteremoji (string source) {
if (!containsemoji (source)) {
Return source;//if not included, returns directly
}
It's definitely included here.
StringBuilder buf = null;
int len = Source.length ();
for (int i = 0; i < len; i++) {
Char codepoint = Source.charat (i);
if (Isemojicharacter (codepoint)) {
if (buf = = null) {
BUF = new StringBuilder (Source.length ());
}
Buf.append (codepoint);
} else {
}
}
if (buf = = null) {
Return source;//If no emoji expression is found, the source string is returned
} else {
if (buf.length () = = Len) {//The meaning here is as little as ToString, because the string is regenerated
BUF = null;
return source;
} else {
return buf.tostring ();
}
}
}
}
Implementation class:
Package com.example.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.text.Editable;
Import android.text.Selection;
Import Android.text.TextWatcher;
Import Android.widget.EditText;
Import Android.widget.Toast;
public class Mainactivity extends Activity {
Private String tmp = "";
Whether to reset the contents of the EditText
Private Boolean Resettext;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Final EditText Sign_text = (EditText) Findviewbyid (R.ID.EDITTEXT1);
Sign_text.addtextchangedlistener (New Textwatcher () {
@Override
public void ontextchanged (charsequence s, int start, int arg2, int arg3) {
if (!resettext) {
if ((Arg3 = = 2) && (! Emojifilter.containsemoji (s.tostring (). substring (start, start + 2))) {
Resettext = true;
Sign_text.settext (TMP);
Sign_text.invalidate ();
if (Sign_text.gettext (). Length () > 1)
Selection.setselection (Sign_text.gettext (), Sign_text.gettext (). Length ());
Toast.maketext (mainactivity.this, "expression input not supported", 0). Show ();
}
} else {
Resettext = false;
}
}
@Override
public void beforetextchanged (charsequence arg0, int arg1, int arg2, int arg3) {
if (!resettext)
TMP = Arg0.tostring ();
}
@Override
public void aftertextchanged (Editable arg0) {
TODO auto-generated Method Stub
}
});
}
}
Android Filter emoticons, no one