From 05c61ba039a1409823e6b1377c2fc433e48af8ee Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 28 Apr 2015 03:03:09 +0800 Subject: [PATCH] added length checker --- .../MaterialAutoCompleteTextView.java | 27 ++++++--- .../materialedittext/MaterialEditText.java | 8 +-- .../MaterialMultiAutoCompleteTextView.java | 57 +++++++++++-------- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java index ba05d61f..c5d873ae 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java @@ -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; @@ -310,6 +311,7 @@ public class MaterialAutoCompleteTextView extends AutoCompleteTextView { OnFocusChangeListener innerFocusChangeListener; OnFocusChangeListener outerFocusChangeListener; private List validators; + private METLengthChecker lengthChecker; public MaterialAutoCompleteTextView(Context context) { super(context); @@ -1207,6 +1209,10 @@ public List getValidators() { return this.validators; } + public void setLengthChecker(METLengthChecker lengthChecker) { + this.lengthChecker = lengthChecker; + } + @Override public void setOnFocusChangeListener(OnFocusChangeListener listener) { if (innerFocusChangeListener == null) { @@ -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); @@ -1393,7 +1399,7 @@ private int getBottomTextRightOffset() { } private int getCharactersCounterWidth() { - return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; + return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; } private int getBottomEllipsisWidth() { @@ -1401,11 +1407,11 @@ private int getBottomEllipsisWidth() { } 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)); } } @@ -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; } @@ -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); + } } \ No newline at end of file diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java index 7dbab598..43a0db50 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java @@ -1310,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); @@ -1399,7 +1399,7 @@ private int getBottomTextRightOffset() { } private int getCharactersCounterWidth() { - return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; + return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; } private int getBottomEllipsisWidth() { @@ -1407,7 +1407,7 @@ private int getBottomEllipsisWidth() { } private void checkCharactersCount() { - if (!hasCharatersCounter()) { + if (!hasCharactersCounter()) { charactersCountValid = true; } else { CharSequence text = getText(); @@ -1420,7 +1420,7 @@ public boolean isCharactersCountValid() { return charactersCountValid; } - private boolean hasCharatersCounter() { + private boolean hasCharactersCounter() { return minCharacters > 0 || maxCharacters > 0; } diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java index 7be4859b..94f8cf86 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java @@ -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; @@ -307,6 +308,7 @@ public class MaterialMultiAutoCompleteTextView extends MultiAutoCompleteTextView OnFocusChangeListener innerFocusChangeListener; OnFocusChangeListener outerFocusChangeListener; private List validators; + private METLengthChecker lengthChecker; public MaterialMultiAutoCompleteTextView(Context context) { super(context); @@ -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(); - } }); } @@ -1204,6 +1206,10 @@ public List getValidators() { return this.validators; } + public void setLengthChecker(METLengthChecker lengthChecker) { + this.lengthChecker = lengthChecker; + } + @Override public void setOnFocusChangeListener(OnFocusChangeListener listener) { if (innerFocusChangeListener == null) { @@ -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); @@ -1390,7 +1396,7 @@ private int getBottomTextRightOffset() { } private int getCharactersCounterWidth() { - return hasCharatersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; + return hasCharactersCounter() ? (int) textPaint.measureText(getCharactersCounterText()) : 0; } private int getBottomEllipsisWidth() { @@ -1398,11 +1404,11 @@ private int getBottomEllipsisWidth() { } 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)); } } @@ -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; } @@ -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); + } } \ No newline at end of file