Skip to content

Commit

Permalink
Merge pull request rengwuxian#173 from mariotaku/master
Browse files Browse the repository at this point in the history
Support custom length validator
  • Loading branch information
rengwuxian committed Apr 28, 2015
2 parents d33a356 + 05c61ba commit da02d43
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 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 @@ -310,6 +311,7 @@ public class MaterialAutoCompleteTextView extends AppCompatAutoCompleteTextView
OnFocusChangeListener innerFocusChangeListener;
OnFocusChangeListener outerFocusChangeListener;
private List<METValidator> validators;
private METLengthChecker lengthChecker;

public MaterialAutoCompleteTextView(Context context) {
super(context);
Expand Down Expand Up @@ -1207,6 +1209,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 @@ -1304,7 +1310,7 @@ protected void onDraw(@NonNull Canvas canvas) {
float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;

// draw the characters counter
if ((hasFocus() && hasCharatersCounter()) || !isCharactersCountValid()) {
if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
textPaint.setColor(isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
String charactersCounterText = getCharactersCounterText();
canvas.drawText(charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint);
Expand Down Expand Up @@ -1393,19 +1399,19 @@ private int getBottomTextRightOffset() {
}

private int getCharactersCounterWidth() {
return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
}

private int getBottomEllipsisWidth() {
return singleLineEllipsis ? (bottomEllipsisSize * 5 + getPixel(4)) : 0;
}

private void checkCharactersCount() {
if (!hasCharatersCounter()) {
if (!hasCharactersCounter()) {
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 @@ -1414,18 +1420,18 @@ public boolean isCharactersCountValid() {
return charactersCountValid;
}

private boolean hasCharatersCounter() {
private boolean hasCharactersCounter() {
return minCharacters > 0 || maxCharacters > 0;
}

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 @@ -1488,4 +1494,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
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 @@ -310,6 +311,7 @@ public class MaterialEditText extends AppCompatEditText {
OnFocusChangeListener innerFocusChangeListener;
OnFocusChangeListener outerFocusChangeListener;
private List<METValidator> validators;
private METLengthChecker lengthChecker;

public MaterialEditText(Context context) {
super(context);
Expand Down Expand Up @@ -1207,6 +1209,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 @@ -1304,7 +1310,7 @@ protected void onDraw(@NonNull Canvas canvas) {
float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;

// draw the characters counter
if ((hasFocus() && hasCharatersCounter()) || !isCharactersCountValid()) {
if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
textPaint.setColor(isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
String charactersCounterText = getCharactersCounterText();
canvas.drawText(charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint);
Expand Down Expand Up @@ -1393,19 +1399,19 @@ private int getBottomTextRightOffset() {
}

private int getCharactersCounterWidth() {
return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
}

private int getBottomEllipsisWidth() {
return singleLineEllipsis ? (bottomEllipsisSize * 5 + getPixel(4)) : 0;
}

private void checkCharactersCount() {
if (!hasCharatersCounter()) {
if (!hasCharactersCounter()) {
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 @@ -1414,18 +1420,18 @@ public boolean isCharactersCountValid() {
return charactersCountValid;
}

private boolean hasCharatersCounter() {
private boolean hasCharactersCounter() {
return minCharacters > 0 || maxCharacters > 0;
}

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 @@ -1488,4 +1494,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
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 @@ -307,6 +308,7 @@ public class MaterialMultiAutoCompleteTextView extends AppCompatMultiAutoComplet
OnFocusChangeListener innerFocusChangeListener;
OnFocusChangeListener outerFocusChangeListener;
private List<METValidator> validators;
private METLengthChecker lengthChecker;

public MaterialMultiAutoCompleteTextView(Context context) {
super(context);
Expand Down Expand Up @@ -456,24 +458,24 @@ private void initText() {

private void initTextWatcher() {
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
checkCharactersCount();
if (autoValidate) {
validate();
} else {
setError(null);
@Override
public void afterTextChanged(Editable s) {
checkCharactersCount();
if (autoValidate) {
validate();
} else {
setError(null);
}
postInvalidate();
}
postInvalidate();
}
});
}

Expand Down Expand Up @@ -1204,6 +1206,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 @@ -1301,7 +1307,7 @@ protected void onDraw(@NonNull Canvas canvas) {
float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;

// draw the characters counter
if ((hasFocus() && hasCharatersCounter()) || !isCharactersCountValid()) {
if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
textPaint.setColor(isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
String charactersCounterText = getCharactersCounterText();
canvas.drawText(charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint);
Expand Down Expand Up @@ -1390,19 +1396,19 @@ private int getBottomTextRightOffset() {
}

private int getCharactersCounterWidth() {
return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0;
}

private int getBottomEllipsisWidth() {
return singleLineEllipsis ? (bottomEllipsisSize * 5 + getPixel(4)) : 0;
}

private void checkCharactersCount() {
if (!hasCharatersCounter()) {
if (!hasCharactersCounter()) {
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 @@ -1411,18 +1417,18 @@ public boolean isCharactersCountValid() {
return charactersCountValid;
}

private boolean hasCharatersCounter() {
private boolean hasCharactersCounter() {
return minCharacters > 0 || maxCharacters > 0;
}

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 @@ -1485,4 +1491,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 da02d43

Please sign in to comment.