diff --git a/README.md b/README.md index ecead47..49ef29c 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ pinView.setItemBackground(getResources().getDrawable(R.drawable.item_background) pinView.setItemBackgroundResources(R.drawable.item_background); pinView.setHideLineWhenFilled(false); pinView.setPasswordHidden(false); +pinView.setError(false); pinView.setTransformationMethod(new PasswordTransformationMethod()); ``` diff --git a/pinview/src/main/java/com/chaos/view/PinView.java b/pinview/src/main/java/com/chaos/view/PinView.java index 9ff2d73..bc4f2ff 100644 --- a/pinview/src/main/java/com/chaos/view/PinView.java +++ b/pinview/src/main/java/com/chaos/view/PinView.java @@ -71,8 +71,9 @@ public class PinView extends AppCompatEditText { private static final InputFilter[] NO_FILTERS = new InputFilter[0]; - private static final int[] HIGHLIGHT_STATES = new int[]{ - android.R.attr.state_selected}; + private static final int[] HIGHLIGHT_STATES = new int[]{android.R.attr.state_selected}; + private static final int[] FILLED_STATES = new int[]{R.attr.state_filled}; + private static final int[] ERROR_STATES = new int[]{R.attr.state_error}; private static final int VIEW_TYPE_RECTANGLE = 0; private static final int VIEW_TYPE_LINE = 1; @@ -103,6 +104,7 @@ public class PinView extends AppCompatEditText { private ValueAnimator mDefaultAddAnimator; private boolean isAnimationEnable = false; private boolean isPasswordHidden; + private boolean isError; private Blink mBlink; private boolean isCursorVisible; @@ -197,6 +199,16 @@ public void setPasswordHidden(boolean hidden) { requestLayout(); } + /** + * Set error state for the PinView + * + * @param error True to enable the error state, false to remove the error state + */ + public void setError(boolean error) { + isError = error; + requestLayout(); + } + /** * new behavior: reveal or hide the pins programmatically * @@ -369,7 +381,16 @@ private void drawPinView(Canvas canvas) { int highlightIdx = getText().length(); for (int i = 0; i < mPinItemCount; i++) { boolean highlight = isFocused() && highlightIdx == i; - mPaint.setColor(highlight ? getLineColorForState(HIGHLIGHT_STATES) : mCurLineColor); + boolean filled = highlightIdx > i; + if (isError) { + mPaint.setColor(getLineColorForState(ERROR_STATES)); + } else { + if (highlight) { + mPaint.setColor(getLineColorForState(HIGHLIGHT_STATES)); + } else { + mPaint.setColor(filled ? getLineColorForState(FILLED_STATES) : mCurLineColor); + } + } updateItemRectF(i); updateCenterPoint(); @@ -901,6 +922,7 @@ public void setTextSize(int unit, float size) { } //region ItemBackground + /** * Set the item background to a given resource. The resource should refer to * a Drawable object or 0 to remove the item background. diff --git a/pinview/src/main/res/values/attrs.xml b/pinview/src/main/res/values/attrs.xml index 135d002..d09a937 100644 --- a/pinview/src/main/res/values/attrs.xml +++ b/pinview/src/main/res/values/attrs.xml @@ -1,5 +1,4 @@ - - - + + + diff --git a/sample/src/main/res/values/colors.xml b/sample/src/main/res/values/colors.xml index d0db3f4..2a3df55 100644 --- a/sample/src/main/res/values/colors.xml +++ b/sample/src/main/res/values/colors.xml @@ -27,4 +27,6 @@ #E91E63 #3F51B5 #C5CAE9 + #C2D422 + #FFDE1A