Back to project page ExpertAndroid.
The source code is released under:
MIT License
If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.iuriio.demos.expertandroid.ch6forms; // w w w . j a va2 s . c o m import android.widget.TextView; import java.util.ArrayList; /** * Created by iuriio on 10/24/13. */ public class Field implements IValidator { private TextView control; private boolean required = true; private ArrayList<IValueValidator> valueValidatorList = new ArrayList<IValueValidator>(); public Field(TextView tv) { this(tv, true); } public Field(TextView tv, boolean required) { this.control = tv; this.required = required; } public void addValidator(IValueValidator validator) { this.valueValidatorList.add(validator); } @Override public boolean validate() { String value = this.getValue(); if (StringUtils.invalidString(value)) { if (this.required) { this.warnRequiredField(); return false; } } for (IValueValidator validator : this.valueValidatorList) { boolean result = validator.validateValue(this.getValue()); if (result) { continue; } String errorMessage = validator.getErrorMessage(); this.setErrorMessage(errorMessage); return false; } return true; } private void warnRequiredField() { this.setErrorMessage("This is a required field"); } public void setErrorMessage(String message) { this.control.setError(message); } public String getValue() { return this.control.getText().toString(); } }