android EditText 禁止表情输入
在转载的基础上,根据自己的需求稍作改动。比如限制最多 Configure.INPUTMAX_LIMIT 个字符的输入
不是自己输入,而是从其他地方复制过后剪贴板本来带有表情文字,对粘贴时稍作了处理
public class ContainsEmojiEditText extends EditText {
//cursor position//txt before inputText;
private String inputAfterText;
//is reset text
private boolean hasReset;
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();
}
private static final int ID_PASTE = android.R.id.paste;
@Override
public boolean onTextContextMenuItem(int id) {
if (id == ID_PASTE) {
hasReset = true;
ClipboardManager clip = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
inputAfterText =""+ clip.getText();
}
return super.onTextContextMenuItem(id);
}
private void initEditText() {
InputFilter[]textFilters=new InputFilter[1];
textFilters[0]=new InputFilter.LengthFilter(Configure.INPUTMAX_LIMIT){
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source.length() > 0 && dest.length()==Configure.INPUTMAX_LIMIT) {
CustomToast.showTextToast(R.string.input_text_limit, mContext);
}
return super.filter(source, start, end, dest, dstart, dend);
}
};
setFilters(textFilters);
addTextChangedListener(new TextWatcher() {
private final int charMaxNum = Configure.INPUTMAX_LIMIT;
int cursorPos;
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
cursorPos = s.length();
if (hasReset) {
hasReset = false;
}else{
inputAfterText = s.toString();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (cursorPos < s.length()) {
if(start+before < start + count){
CharSequence input = s.subSequence(start+before, start + count);
if (containsEmoji(input.toString())) {
CustomToast.showTextToast(R.string.not_support, mContext);
setText(inputAfterText);
resetCursor(start+before);
}
}
}
}
@Override
public void afterTextChanged(Editable editable) {
int length = getText().length();
if (lisen != null) {
lisen.update(length);
}
}
});
}
private void resetCursor(int index ) {
CharSequence text = getText();
if (text instanceof Spannable) {
Spannable spanText = (Spannable) text;
Selection.setSelection(spanText, index);
}
}
public interface OnEmojiEditLisen {
void update(int length);
}
private OnEmojiEditLisen lisen;
public void setOnEmojiEditLisen(OnEmojiEditLisen lisen) {
this.lisen = lisen;
}/**
* check 'emoji' isExit
*
* @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)) {
return true;
}
}
return false;
}
/**
* check is or not '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));
}
}