EditText限制輸入字數,並且禁止輸入表情符號
阿新 • • 發佈:2019-02-04
場景:在EditText輸入表情符號,但傳送到伺服器之後,變成了問好,因為伺服器無法識別。
解決方案:ContainsEmojiEditText,通過輸入字元的長度,判斷是否為表情符號。
import android.content.Context; import android.text.Editable; import android.text.Selection; import android.text.Spannable; import android.text.TextWatcher; import android.util.AttributeSet; import android.widget.EditText; import android.widget.Toast; public class ContainsEmojiEditText extends EditText { //輸入表情前的游標位置 private int cursorPos; //輸入表情前EditText中的文字 private String inputAfterText; //是否重置了EditText的內容 private boolean resetText; 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(); } // 初始化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) {//表情符號的字元長度最小為2 CharSequence input = s.subSequence(cursorPos, cursorPos + count); if (containsEmoji(input.toString())) { resetText = true; Toast.makeText(mContext, "不支援輸入Emoji表情符號", Toast.LENGTH_SHORT).show(); //是表情符號就將文字還原為輸入表情符號之前的內容 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)); } }
在佈局檔案中定義:
<com.ak.qmyd.view.ContainsEmojiEditText android:id="@+id/et_content_edit" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/editext_jianjie_border" android:drawableLeft="@drawable/edit_big" android:drawablePadding="10dp" android:gravity="top" android:hint="說點什麼" android:imeOptions="actionSend" android:inputType="text" android:padding="10dp" android:singleLine="true" />
在Activity中實現輸入監聽,用於判斷輸入長度.
et_content_note_edit.addTextChangedListener(new TextWatcher() { private int cou = 0; int selectionEnd = 0; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { cou = before + count; String editable = et_content_note_edit.getText().toString(); String str = StringUtil.stringFilter(editable); // 過濾特殊字元 if (!editable.equals(str)) { et_content_note_edit.setText(str); } et_content_note_edit.setSelection(et_content_note_edit.length()); cou = et_content_note_edit.length(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (cou > mMaxLenth) { selectionEnd = et_content_note_edit.getSelectionEnd(); s.delete(mMaxLenth, selectionEnd); ToastManager.show(DongTaiDetailActivity.this, "評論內容不能超過200個字元"); } } });
ok,這樣就可以實時監聽輸入的長度,也可以限制表情符號的輸入了。