Skip to content

Commit

Permalink
added custom length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Apr 27, 2015
1 parent be868ad commit 778b946
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.rengwuxian.materialedittext.validation.METLengthChecker;
import com.rengwuxian.materialedittext.validation.METValidator;

import java.util.ArrayList;
Expand Down Expand Up @@ -305,6 +306,7 @@ public class MaterialEditText extends EditText {
OnFocusChangeListener innerFocusChangeListener;
OnFocusChangeListener outerFocusChangeListener;
private List<METValidator> validators;
private METLengthChecker lengthChecker;

public MaterialEditText(Context context) {
super(context);
Expand Down Expand Up @@ -1190,6 +1192,10 @@ public List<METValidator> getValidators() {
return this.validators;
}

public void setLengthChecker(METLengthChecker lengthChecker) {
this.lengthChecker = lengthChecker;
}

@Override
public void setOnFocusChangeListener(OnFocusChangeListener listener) {
if (innerFocusChangeListener == null) {
Expand Down Expand Up @@ -1384,7 +1390,7 @@ private void checkCharactersCount() {
charactersCountValid = true;
} else {
CharSequence text = getText();
int count = text == null ? 0 : text.length();
int count = text == null ? 0 : checkLength(text);
charactersCountValid = (count >= minCharacters && (maxCharacters <= 0 || count <= maxCharacters));
}
}
Expand All @@ -1400,11 +1406,11 @@ private boolean hasCharatersCounter() {
private String getCharactersCounterText() {
String text;
if (minCharacters <= 0) {
text = isRTL() ? maxCharacters + " / " + getText().length() : getText().length() + " / " + maxCharacters;
text = isRTL() ? maxCharacters + " / " + checkLength(getText()) : checkLength(getText()) + " / " + maxCharacters;
} else if (maxCharacters <= 0) {
text = isRTL() ? "+" + minCharacters + " / " + getText().length() : getText().length() + " / " + minCharacters + "+";
text = isRTL() ? "+" + minCharacters + " / " + checkLength(getText()) : checkLength(getText()) + " / " + minCharacters + "+";
} else {
text = isRTL() ? maxCharacters + "-" + minCharacters + " / " + getText().length() : getText().length() + " / " + minCharacters + "-" + maxCharacters;
text = isRTL() ? maxCharacters + "-" + minCharacters + " / " + checkLength(getText()) : checkLength(getText()) + " / " + minCharacters + "-" + maxCharacters;
}
return text;
}
Expand Down Expand Up @@ -1467,4 +1473,9 @@ private boolean insideClearButton(MotionEvent event) {
int buttonTop = getScrollY() + getHeight() - getPaddingBottom() + bottomSpacing - iconOuterHeight;
return (x >= buttonLeft && x < buttonLeft + iconOuterWidth && y >= buttonTop && y < buttonTop + iconOuterHeight);
}

private int checkLength(CharSequence text) {
if (lengthChecker==null) return text.length();
return lengthChecker.getLength(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rengwuxian.materialedittext.validation;

/**
* Created by mariotaku on 15/4/12.
*/
public abstract class METLengthChecker {

public abstract int getLength(CharSequence text);

}

0 comments on commit 778b946

Please sign in to comment.