From 7ab9124d05ca89f7b15340b18a1bb4c8be06a6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=94=E7=89=A9=E7=BA=BF?= Date: Sun, 18 Jan 2015 02:33:12 +0800 Subject: [PATCH] New options: floatingLabelAlwaysShown and helperTextAlwaysShown. --- .../MaterialAutoCompleteTextView.java | 42 +++++++++++++++-- .../materialedittext/MaterialEditText.java | 45 ++++++++++++++++--- .../MaterialMultiAutoCompleteTextView.java | 42 +++++++++++++++-- library/src/main/res/values/attrs.xml | 4 ++ 4 files changed, 118 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java index 3d169fc9..bde3b084 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java @@ -155,6 +155,16 @@ public class MaterialAutoCompleteTextView extends AutoCompleteTextView { */ private boolean singleLineEllipsis; + /** + * Always show the floating label, instead of animating it in/out. False by default. + */ + private boolean floatingLabelAlwaysShown; + + /** + * Always show the helper text, no matter if the edit text is focused. False by default. + */ + private boolean helperTextAlwaysShown; + /** * bottom ellipsis's height */ @@ -344,6 +354,9 @@ private void init(Context context, AttributeSet attrs) { iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1)); iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1)); iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8)); + floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false); + helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false); + typedArray.recycle(); int[] paddings = new int[]{ android.R.attr.padding, // 0 @@ -358,7 +371,7 @@ private void init(Context context, AttributeSet attrs) { innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding); innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding); innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding); - typedArray.recycle(); + paddingsTypedArray.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(null); @@ -382,9 +395,12 @@ private void initText() { if (!TextUtils.isEmpty(getText())) { CharSequence text = getText(); setText(null); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); setText(text); floatingLabelFraction = 1; floatingLabelShown = true; + } else { + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); } } @@ -518,6 +534,24 @@ public void setCurrentBottomLines(float currentBottomLines) { initPadding(); } + public boolean getFloatingLabelAlwaysShown() { + return floatingLabelAlwaysShown; + } + + public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) { + this.floatingLabelAlwaysShown = floatingLabelAlwaysShown; + invalidate(); + } + + public boolean getHelperTextAlwaysShown() { + return helperTextAlwaysShown; + } + + public void setHelperText(boolean helperTextAlwaysShown) { + this.helperTextAlwaysShown = helperTextAlwaysShown; + invalidate(); + } + @Nullable public Typeface getAccentTypeface() { return accentTypeface; @@ -1076,7 +1110,7 @@ protected void onDraw(@NonNull Canvas canvas) { // draw the bottom text if (textLayout != null) { - if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text + if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor()); canvas.save(); canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding); @@ -1105,10 +1139,10 @@ protected void onDraw(@NonNull Canvas canvas) { // calculate the vertical position int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing; int distance = floatingLabelSpacing; - int position = (int) (floatingLabelStartY - distance * floatingLabelFraction); + int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction)); // calculate the alpha - int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f)); + int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f)); textPaint.setAlpha(alpha); // draw the floating label diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java index 15fbec0d..c1fa754d 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java @@ -28,15 +28,12 @@ import android.view.Gravity; import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; import android.widget.EditText; import com.nineoldandroids.animation.ArgbEvaluator; import com.nineoldandroids.animation.ObjectAnimator; import com.rengwuxian.materialedittext.validation.METValidator; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -158,6 +155,16 @@ public class MaterialEditText extends EditText { */ private boolean singleLineEllipsis; + /** + * Always show the floating label, instead of animating it in/out. False by default. + */ + private boolean floatingLabelAlwaysShown; + + /** + * Always show the helper text, no matter if the edit text is focused. False by default. + */ + private boolean helperTextAlwaysShown; + /** * bottom ellipsis's height */ @@ -347,6 +354,9 @@ private void init(Context context, AttributeSet attrs) { iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1)); iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1)); iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8)); + floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false); + helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false); + typedArray.recycle(); int[] paddings = new int[]{ android.R.attr.padding, // 0 @@ -361,7 +371,7 @@ private void init(Context context, AttributeSet attrs) { innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding); innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding); innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding); - typedArray.recycle(); + paddingsTypedArray.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(null); @@ -385,9 +395,12 @@ private void initText() { if (!TextUtils.isEmpty(getText())) { CharSequence text = getText(); setText(null); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); setText(text); floatingLabelFraction = 1; floatingLabelShown = true; + } else { + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); } } @@ -521,6 +534,24 @@ public void setCurrentBottomLines(float currentBottomLines) { initPadding(); } + public boolean getFloatingLabelAlwaysShown() { + return floatingLabelAlwaysShown; + } + + public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) { + this.floatingLabelAlwaysShown = floatingLabelAlwaysShown; + invalidate(); + } + + public boolean getHelperTextAlwaysShown() { + return helperTextAlwaysShown; + } + + public void setHelperText(boolean helperTextAlwaysShown) { + this.helperTextAlwaysShown = helperTextAlwaysShown; + invalidate(); + } + @Nullable public Typeface getAccentTypeface() { return accentTypeface; @@ -1079,7 +1110,7 @@ protected void onDraw(@NonNull Canvas canvas) { // draw the bottom text if (textLayout != null) { - if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text + if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor()); canvas.save(); canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding); @@ -1108,10 +1139,10 @@ protected void onDraw(@NonNull Canvas canvas) { // calculate the vertical position int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing; int distance = floatingLabelSpacing; - int position = (int) (floatingLabelStartY - distance * floatingLabelFraction); + int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction)); // calculate the alpha - int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f)); + int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f)); textPaint.setAlpha(alpha); // draw the floating label diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java index 9190d5fd..116071e5 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java @@ -152,6 +152,16 @@ public class MaterialMultiAutoCompleteTextView extends MultiAutoCompleteTextView */ private boolean singleLineEllipsis; + /** + * Always show the floating label, instead of animating it in/out. False by default. + */ + private boolean floatingLabelAlwaysShown; + + /** + * Always show the helper text, no matter if the edit text is focused. False by default. + */ + private boolean helperTextAlwaysShown; + /** * bottom ellipsis's height */ @@ -341,6 +351,9 @@ private void init(Context context, AttributeSet attrs) { iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconLeft, -1)); iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_iconRight, -1)); iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_iconPadding, getPixel(8)); + floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_floatingLabelAlwaysShown, false); + helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_helperTextAlwaysShown, false); + typedArray.recycle(); int[] paddings = new int[]{ android.R.attr.padding, // 0 @@ -355,7 +368,7 @@ private void init(Context context, AttributeSet attrs) { innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding); innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding); innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding); - typedArray.recycle(); + paddingsTypedArray.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(null); @@ -379,9 +392,12 @@ private void initText() { if (!TextUtils.isEmpty(getText())) { CharSequence text = getText(); setText(null); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); setText(text); floatingLabelFraction = 1; floatingLabelShown = true; + } else { + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); } } @@ -515,6 +531,24 @@ public void setCurrentBottomLines(float currentBottomLines) { initPadding(); } + public boolean getFloatingLabelAlwaysShown() { + return floatingLabelAlwaysShown; + } + + public void setFloatingLabelAlwaysShown(boolean floatingLabelAlwaysShown) { + this.floatingLabelAlwaysShown = floatingLabelAlwaysShown; + invalidate(); + } + + public boolean getHelperTextAlwaysShown() { + return helperTextAlwaysShown; + } + + public void setHelperText(boolean helperTextAlwaysShown) { + this.helperTextAlwaysShown = helperTextAlwaysShown; + invalidate(); + } + @Nullable public Typeface getAccentTypeface() { return accentTypeface; @@ -1073,7 +1107,7 @@ protected void onDraw(@NonNull Canvas canvas) { // draw the bottom text if (textLayout != null) { - if (tempErrorText != null || (hasFocus() && !TextUtils.isEmpty(helperText))) { // error text or helper text + if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : getCurrentHintTextColor()); canvas.save(); canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding); @@ -1102,10 +1136,10 @@ protected void onDraw(@NonNull Canvas canvas) { // calculate the vertical position int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing; int distance = floatingLabelSpacing; - int position = (int) (floatingLabelStartY - distance * floatingLabelFraction); + int position = (int) (floatingLabelStartY - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction)); // calculate the alpha - int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f)); + int alpha = (int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction + 0.26f)); textPaint.setAlpha(alpha); // draw the floating label diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 26886ba1..ecd7ce5e 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -45,5 +45,9 @@ + + + + \ No newline at end of file