Edittext禁止輸入空格換行以及特殊符號
阿新 • • 發佈:2018-11-19
/** * 禁止EditText輸入空格和換行符以及特殊符號 * * @param editText EditText輸入框 */ public static void setEditTextInputSpace(EditText editText) { InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { String speChat = "[`
[email protected]#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern pattern = Pattern.compile(speChat); Matcher matcher = pattern.matcher(source.toString()); if (source.equals(" ") || source.toString().contentEquals("\n") || matcher.find()) { return ""; } else { return null; } } }; editText.setFilters(new InputFilter[]{filter}); }