EditText 禁止输入空格 换行

/**
 * 禁止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) {
            if (source.equals(" ") || source.toString().contentEquals("\n")) {
                return "";
            } else {
                return null;
            }
        }
    };
    editText.setFilters(new InputFilter[]{filter});
}

 

你可能感兴趣的:(EditText 禁止输入空格 换行)