MaterialEditText RegexpValidator

时间:2017-10-29 19:39:54

标签: java android regex

如果输入有效而不是charSequence.length,我如何让这段代码检查输入是否有效并设置z2 = true?

我正在使用这个lib:

https://github.com/rengwuxian/MaterialEditText

    this.inputDisplayName.addValidator(new RegexpValidator("You can only use Laters and Numbers", "^[a-zA-Z0-9]*$"){
        public boolean isValid(CharSequence charSequence, boolean z) {
            boolean z2 = false;
            if (TextUtils.isEmpty(charSequence)) {
                RegisterActivity.this.displaynameVerified = false;
            } else {
                RegisterActivity registerActivity = RegisterActivity.this;
                if (charSequence.length() >= 8) {
                    z2 = true;
                }
                registerActivity.displaynameVerified = z2;
            }
            RegisterActivity.this.validateInput();
            return true;
        }
    });

1 个答案:

答案 0 :(得分:0)

你完全重写RegexValidation,不对正则表达式做任何事情。

就个人而言,我建议你扩展它并使用界面添加回调。

/*
* Add these inside the class
*/

private static interface ValidCallback {
    public void isValid(Boolean valid);
}

private static class NumberLetterValidator extends RegexpValidator { 

    private ValidCallback c;

    public NumberLetterValidator(ValidCallback c) {
        super("You can only use Laters and Numbers", "^[a-zA-Z0-9]*$");
        this.c = c;
    }

    @Override
    public boolean isValid(CharSequence charSequence, boolean isEmpty) {
        boolean valid = super.isValid(charSequence, isEmpty);
        if (c != null) c.isValid(valid);
        return valid;
    }
}

然后,您可以通过那里传递自定义逻辑。

ValidCallback cb = new ValidCallback() {
    @Override
    public void isValid(Boolean b) {
        displaynameVerified = b;   
        validateInput();
    }
};
this.inputDisplayName.addValidator(new NumberLetterValidator(cb));