android中的EditTex验证

时间:2016-08-11 11:00:59

标签: android validation android-edittext

我的应用程序中有一个Edittext,我希望用户不要输入第一个字符作为空格。但是在输入其他字符之后,用户可以输入空格,我该怎么做>我有这段代码:



 android:id="@+id/referralCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:background="@drawable/formfield_1"
                android:hint="@string/referralcode_hint"
                android:imeOptions="actionNext"/>




在java类中我有一个输入过滤器:



  public static InputFilter alphabetsFilter = new InputFilter() {

	@Override
	public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
	    if (source.equals("")) { // for backspace
		return source;
	    }
	    if (source.toString().matches("[a-zA-Z ]+")) {
		return source;
	    }
	    return "";
	}
    };




不允许第一个空白字符,但我希望它允许输入数字和特殊字符

4 个答案:

答案 0 :(得分:1)

您可以像这样制作CustomEditText类...

public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (text.length() == 1) {
            if (Character.isSpaceChar(text.charAt(0))) {
                this.setText("");
            }
        }
    }
}

而不是xml中的EditText使用此CustomEditText

答案 1 :(得分:1)

你可以做的是设置addTextChangedListener,如下所示:

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
          if(!(s.toString().charAt(0) == ' ')) {
             //do whatever you want
             //you can show an alert dialog saying the first character should not be a whitespace
           }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

答案 2 :(得分:0)

你可以试试这个:

inal EditText editText = (EditText)findViewById(R.id.editText);


    InputFilter filter = new InputFilter() { 
        boolean canEnterSpace = false;

        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {

            if(editText.getText().toString().equals(""))
            { 
                canEnterSpace = false;
            } 

            StringBuilder builder = new StringBuilder();

            for (int i = start; i < end; i++) { 
                char currentChar = source.charAt(i);

                if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
                    builder.append(currentChar);
                    canEnterSpace = true;
                } 

                if(Character.isWhitespace(currentChar) && canEnterSpace) {
                    builder.append(currentChar);
                } 


            } 
            return builder.toString();          
        } 

    }; 


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

答案 3 :(得分:0)

在输入文件管理器中循环,并添加所需的字符,如:

private static class AlphaNumericInputFilter implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        // Only keep characters that are alphanumeric
        StringBuilder builder = new StringBuilder();
        for (int i = start; i < end; i++) {
            char c = source.charAt(i);
            if (Character.isLetterOrDigit(c) || !Character.isSpaceChar(c)) {
                builder.append(c);
            }
        }
        // If all characters are valid, return null, otherwise only return the filtered
        // characters
        boolean allCharactersValid = (builder.length() == end - start);
        return allCharactersValid ? null : builder.toString();
    }
}

并创建此过滤器的新实例,将其添加到edittext。