将InputFilter设置为空白和最大长度

时间:2016-09-27 11:05:50

标签: android validation android-edittext input-filter

我想使用输入过滤器,我可以阻止第一个空格输入和编辑文本的最大长度限制为200个字符。

到目前为止我有这个: 我将maxlength放入同一个过滤器



 private void setInputFilterForEmailAndPwd(final EditText amountEditText) {
	InputFilter filter = new InputFilter() {
	    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
		for (int i = start; i < end; i++) {
		    if (Character.isSpace(source.charAt(i))) {
			return "";
		    }
		}
		
		return null;
	    }
	};

	amountEditText.setFilters(new InputFilter[] { filter });
    }



setInputFilterForEmailAndPwd(emailEdit);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我找到了解决方案 我把新的输入过滤器,而不是setInputFilterForEmailAndPwd:

&#13;
&#13;
InputFilter filter = new InputFilter() {
	    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
		for (int i = start; i < end; i++) {
		    if (Character.isWhitespace(source.charAt(i))) {
			return "";
		    }
		}
		return null;
	    }
	};
&#13;
&#13;
&#13;

然后:

&#13;
&#13;
	emailEdit.setFilters(new InputFilter[] { filter, new InputFilter.LengthFilter(200) });
&#13;
&#13;
&#13;