diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java index 172861c4..3d169fc9 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java @@ -292,8 +292,7 @@ private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, defaultBaseColor); - ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); - setTextColor(colorStateList); + setBaseColor(baseColor); // retrieve the default primaryColor int defaultPrimaryColor; @@ -383,12 +382,9 @@ 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); } } @@ -598,11 +594,6 @@ private void initPadding() { extraPaddingBottom = (int) ((textMetrics.descent - textMetrics.ascent) * currentBottomLines) + (hideUnderline ? bottomSpacing : bottomSpacing * 2); extraPaddingLeft = iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding); extraPaddingRight = iconRightBitmaps == null ? 0 : (iconOuterWidth + iconPadding); - - System.out.println("left:" + getPaddingLeft() + ", extraPaddingLeft:" + extraPaddingLeft); - System.out.println("right:" + getPaddingRight() + ", extraPaddingRight:" + extraPaddingRight); - System.out.println("top:" + getPaddingTop() + ", extraPaddingTop:" + extraPaddingTop); - System.out.println("bottom:" + getPaddingBottom() + ", extraPaddingBottom:" + extraPaddingBottom); setPaddings(innerPaddingLeft, innerPaddingTop, innerPaddingRight, innerPaddingBottom); } @@ -697,19 +688,19 @@ public int getInnerPaddingRight() { } private void initFloatingLabel() { - if (floatingLabelEnabled) { - // observe the text changing - addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } + // observe the text changing + addTextChangedListener(new TextWatcher() { + @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) { + @Override + public void afterTextChanged(Editable s) { + if (floatingLabelEnabled) { if (s.length() == 0) { if (floatingLabelShown) { floatingLabelShown = false; @@ -724,34 +715,38 @@ public void afterTextChanged(Editable s) { } } } - }); - if (highlightFloatingLabel) { - // observe the focus state to animate the floating label's text color appropriately - innerFocusChangeListener = new OnFocusChangeListener() { - @Override - public void onFocusChange(View v, boolean hasFocus) { - if (hasFocus) { - if (getLabelFocusAnimator().isStarted()) { - getLabelFocusAnimator().reverse(); - } else { - getLabelFocusAnimator().start(); - } - } else { + } + }); + // observe the focus state to animate the floating label's text color appropriately + innerFocusChangeListener = new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (floatingLabelEnabled && highlightFloatingLabel) { + if (hasFocus) { + if (getLabelFocusAnimator().isStarted()) { getLabelFocusAnimator().reverse(); + } else { + getLabelFocusAnimator().start(); } - if (outerFocusChangeListener != null) { - outerFocusChangeListener.onFocusChange(v, hasFocus); - } + } else { + getLabelFocusAnimator().reverse(); + } + if (outerFocusChangeListener != null) { + outerFocusChangeListener.onFocusChange(v, hasFocus); } - }; - super.setOnFocusChangeListener(innerFocusChangeListener); + } } - } - + }; + super.setOnFocusChangeListener(innerFocusChangeListener); } public void setBaseColor(int color) { - baseColor = color; + if (baseColor != color) { + baseColor = color; + } + ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); + setTextColor(colorStateList); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); postInvalidate(); } @@ -779,7 +774,7 @@ private void setFloatingLabelInternal(int mode) { public void setFloatingLabel(@FloatingLabelType int mode) { setFloatingLabelInternal(mode); - postInvalidate(); + initPadding(); } public int getFloatingLabelSpacing() { @@ -1054,7 +1049,7 @@ protected void onDraw(@NonNull Canvas canvas) { paint.setColor(primaryColor); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint); } else { // normal - paint.setColor(baseColor & 0x00ffffff | 0x44000000); + paint.setColor(baseColor & 0x00ffffff | 0x1E000000); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint); } } diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java index b40be653..15fbec0d 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java @@ -35,6 +35,8 @@ 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; @@ -293,8 +295,7 @@ private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, defaultBaseColor); - ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); - setTextColor(colorStateList); + setBaseColor(baseColor); // retrieve the default primaryColor int defaultPrimaryColor; @@ -384,12 +385,9 @@ 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); } } @@ -599,11 +597,6 @@ private void initPadding() { extraPaddingBottom = (int) ((textMetrics.descent - textMetrics.ascent) * currentBottomLines) + (hideUnderline ? bottomSpacing : bottomSpacing * 2); extraPaddingLeft = iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding); extraPaddingRight = iconRightBitmaps == null ? 0 : (iconOuterWidth + iconPadding); - - System.out.println("left:" + getPaddingLeft() + ", extraPaddingLeft:" + extraPaddingLeft); - System.out.println("right:" + getPaddingRight() + ", extraPaddingRight:" + extraPaddingRight); - System.out.println("top:" + getPaddingTop() + ", extraPaddingTop:" + extraPaddingTop); - System.out.println("bottom:" + getPaddingBottom() + ", extraPaddingBottom:" + extraPaddingBottom); setPaddings(innerPaddingLeft, innerPaddingTop, innerPaddingRight, innerPaddingBottom); } @@ -698,19 +691,19 @@ public int getInnerPaddingRight() { } private void initFloatingLabel() { - if (floatingLabelEnabled) { - // observe the text changing - addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } + // observe the text changing + addTextChangedListener(new TextWatcher() { + @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) { + @Override + public void afterTextChanged(Editable s) { + if (floatingLabelEnabled) { if (s.length() == 0) { if (floatingLabelShown) { floatingLabelShown = false; @@ -725,34 +718,38 @@ public void afterTextChanged(Editable s) { } } } - }); - if (highlightFloatingLabel) { - // observe the focus state to animate the floating label's text color appropriately - innerFocusChangeListener = new OnFocusChangeListener() { - @Override - public void onFocusChange(View v, boolean hasFocus) { - if (hasFocus) { - if (getLabelFocusAnimator().isStarted()) { - getLabelFocusAnimator().reverse(); - } else { - getLabelFocusAnimator().start(); - } - } else { + } + }); + // observe the focus state to animate the floating label's text color appropriately + innerFocusChangeListener = new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (floatingLabelEnabled && highlightFloatingLabel) { + if (hasFocus) { + if (getLabelFocusAnimator().isStarted()) { getLabelFocusAnimator().reverse(); + } else { + getLabelFocusAnimator().start(); } - if (outerFocusChangeListener != null) { - outerFocusChangeListener.onFocusChange(v, hasFocus); - } + } else { + getLabelFocusAnimator().reverse(); + } + if (outerFocusChangeListener != null) { + outerFocusChangeListener.onFocusChange(v, hasFocus); } - }; - super.setOnFocusChangeListener(innerFocusChangeListener); + } } - } - + }; + super.setOnFocusChangeListener(innerFocusChangeListener); } public void setBaseColor(int color) { - baseColor = color; + if (baseColor != color) { + baseColor = color; + } + ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); + setTextColor(colorStateList); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); postInvalidate(); } @@ -780,7 +777,7 @@ private void setFloatingLabelInternal(int mode) { public void setFloatingLabel(@FloatingLabelType int mode) { setFloatingLabelInternal(mode); - postInvalidate(); + initPadding(); } public int getFloatingLabelSpacing() { @@ -1055,7 +1052,7 @@ protected void onDraw(@NonNull Canvas canvas) { paint.setColor(primaryColor); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint); } else { // normal - paint.setColor(baseColor & 0x00ffffff | 0x44000000); + paint.setColor(baseColor & 0x00ffffff | 0x1E000000); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint); } } diff --git a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java index 81085e26..9190d5fd 100644 --- a/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java +++ b/library/src/main/java/com/rengwuxian/materialedittext/MaterialMultiAutoCompleteTextView.java @@ -289,8 +289,7 @@ private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, defaultBaseColor); - ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); - setTextColor(colorStateList); + setBaseColor(baseColor); // retrieve the default primaryColor int defaultPrimaryColor; @@ -380,12 +379,9 @@ 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); } } @@ -595,11 +591,6 @@ private void initPadding() { extraPaddingBottom = (int) ((textMetrics.descent - textMetrics.ascent) * currentBottomLines) + (hideUnderline ? bottomSpacing : bottomSpacing * 2); extraPaddingLeft = iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding); extraPaddingRight = iconRightBitmaps == null ? 0 : (iconOuterWidth + iconPadding); - - System.out.println("left:" + getPaddingLeft() + ", extraPaddingLeft:" + extraPaddingLeft); - System.out.println("right:" + getPaddingRight() + ", extraPaddingRight:" + extraPaddingRight); - System.out.println("top:" + getPaddingTop() + ", extraPaddingTop:" + extraPaddingTop); - System.out.println("bottom:" + getPaddingBottom() + ", extraPaddingBottom:" + extraPaddingBottom); setPaddings(innerPaddingLeft, innerPaddingTop, innerPaddingRight, innerPaddingBottom); } @@ -694,19 +685,19 @@ public int getInnerPaddingRight() { } private void initFloatingLabel() { - if (floatingLabelEnabled) { - // observe the text changing - addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } + // observe the text changing + addTextChangedListener(new TextWatcher() { + @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) { + @Override + public void afterTextChanged(Editable s) { + if (floatingLabelEnabled) { if (s.length() == 0) { if (floatingLabelShown) { floatingLabelShown = false; @@ -721,34 +712,38 @@ public void afterTextChanged(Editable s) { } } } - }); - if (highlightFloatingLabel) { - // observe the focus state to animate the floating label's text color appropriately - innerFocusChangeListener = new OnFocusChangeListener() { - @Override - public void onFocusChange(View v, boolean hasFocus) { - if (hasFocus) { - if (getLabelFocusAnimator().isStarted()) { - getLabelFocusAnimator().reverse(); - } else { - getLabelFocusAnimator().start(); - } - } else { + } + }); + // observe the focus state to animate the floating label's text color appropriately + innerFocusChangeListener = new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (floatingLabelEnabled && highlightFloatingLabel) { + if (hasFocus) { + if (getLabelFocusAnimator().isStarted()) { getLabelFocusAnimator().reverse(); + } else { + getLabelFocusAnimator().start(); } - if (outerFocusChangeListener != null) { - outerFocusChangeListener.onFocusChange(v, hasFocus); - } + } else { + getLabelFocusAnimator().reverse(); + } + if (outerFocusChangeListener != null) { + outerFocusChangeListener.onFocusChange(v, hasFocus); } - }; - super.setOnFocusChangeListener(innerFocusChangeListener); + } } - } - + }; + super.setOnFocusChangeListener(innerFocusChangeListener); } public void setBaseColor(int color) { - baseColor = color; + if (baseColor != color) { + baseColor = color; + } + ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); + setTextColor(colorStateList); + setHintTextColor(baseColor & 0x00ffffff | 0x44000000); postInvalidate(); } @@ -776,7 +771,7 @@ private void setFloatingLabelInternal(int mode) { public void setFloatingLabel(@FloatingLabelType int mode) { setFloatingLabelInternal(mode); - postInvalidate(); + initPadding(); } public int getFloatingLabelSpacing() { @@ -1051,7 +1046,7 @@ protected void onDraw(@NonNull Canvas canvas) { paint.setColor(primaryColor); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint); } else { // normal - paint.setColor(baseColor & 0x00ffffff | 0x44000000); + paint.setColor(baseColor & 0x00ffffff | 0x1E000000); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint); } } diff --git a/sample/build.gradle b/sample/build.gradle index 674c9f74..91cd508f 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -20,6 +20,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' - compile 'com.rengwuxian.materialedittext:library:1.8.1' -// compile project(':library') +// compile 'com.rengwuxian.materialedittext:library:1.8.1' + compile project(':library') } diff --git a/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java b/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java index 1441d06a..351b3974 100644 --- a/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java +++ b/sample/src/main/java/com/rengwuxian/materialedittext/sample/MainActivity.java @@ -1,5 +1,6 @@ package com.rengwuxian.materialedittext.sample; +import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.text.Editable; @@ -7,8 +8,10 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import com.rengwuxian.materialedittext.MaterialEditText; import com.rengwuxian.materialedittext.validation.RegexpValidator; @@ -23,11 +26,10 @@ protected void onCreate(Bundle savedInstanceState) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); initEnableBt(); - initGoneEt(); initSingleLineEllipsisEt(); initSetErrorEt(); initValidationEt(); - } + } private void initEnableBt() { final EditText basicEt = (EditText) findViewById(R.id.basicEt); @@ -40,21 +42,6 @@ public void onClick(View v) { } }); } - private void initGoneEt() { - final EditText goneEt = (EditText) findViewById(R.id.goneEt); - goneEt.setError("I was hidden from view!"); - final Button toggleGoneButton = (Button) findViewById(R.id.toggleGoneBt); - toggleGoneButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (goneEt.getVisibility() == View.GONE) { - goneEt.setVisibility(View.VISIBLE); - } else { - goneEt.setVisibility(View.GONE); - } - } - }); - } private void initSingleLineEllipsisEt() { EditText singleLineEllipsisEt = (EditText) findViewById(R.id.singleLineEllipsisEt); @@ -62,18 +49,6 @@ private void initSingleLineEllipsisEt() { } private void initSetErrorEt() { - final EditText title = (EditText) findViewById(R.id.title); - title.addTextChangedListener(new TextWatcher() { - @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 afterTextChanged(Editable s) { - title.setError(s.length() < 8 ? "Too short!" : null); - } - }); - final EditText bottomTextEt = (EditText) findViewById(R.id.bottomTextEt); final Button setErrorBt = (Button) findViewById(R.id.setErrorBt); setErrorBt.setOnClickListener(new View.OnClickListener() {