android非法字元的判定、Emoji的判定

來源:互聯網
上載者:User

標籤:style   emoji表情   super   初始   str   支援   false   att   point   

public class EmojiEditText extends EditText {
// 輸入表情前的游標位置
private int cursorPos; // 輸入表情前EditText中的文本
private String inputAfterText; // 是否重設了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();
} // 初始化edittext 控制項


private void initEditText() {
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int before, int count) {
if (!resetText) {
cursorPos = getSelectionEnd(); // 這裡用s.toString()而不直接用s是因為如果用s,
// 那麼,inputAfterText和s在記憶體中指向的是同一個地址,s改變了,
// inputAfterText也就改變了,那麼表情過濾就失敗了
inputAfterText = s.toString();
}


}


@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!resetText) {
if (count >= 2) {// Emoji的字元長度最小為2
CharSequence input = s.subSequence(cursorPos, cursorPos
+ count);
if (containsEmoji(input.toString())) {
resetText = true;
//暫不支援輸入EmojiEmoji
Toast.makeText(mContext, "暫不支援輸入Emoji",
Toast.LENGTH_SHORT).show(); // 是Emoji就將文本還原為輸入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) {


}
});
}


/**
* 檢測是否有emoji表情

* @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)) { // 如果不能匹配,則該字元是Emoji表情
return true;
}
}
return false;
}


/**
* 判斷是否是Emoji

* @param codePoint
*            比較的單個字元
* @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));
}


}

android非法字元的判定、Emoji的判定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.