简体   繁体   English

MaterialEditText RegexpValidator

[英]MaterialEditText RegexpValidator

How do I get this code to check if the input is valid and set z2 = true, if the input in valid instead of the charSequence.length?如果输入有效而不是charSequence.length,我如何获取此代码以检查输入是否有效并设置z2 = true?

I am using this lib:我正在使用这个库:

https://github.com/rengwuxian/MaterialEditText 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;
        }
    });

You're overriding the RegexValidation entirely to do nothing with the regex given.您完全覆盖了 RegexValidation 以对给定的正则表达式不执行任何操作。

Personally, I would suggest you extend it and add a callback using an interface.就个人而言,我建议您扩展它并使用接口添加回调。

/*
* 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;
    }
}

Then, you can pass your custom logic through there.然后,您可以通过那里传递您的自定义逻辑。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM