1818import android .support .annotation .IntDef ;
1919import android .support .annotation .NonNull ;
2020import android .support .annotation .Nullable ;
21- import android .support .v7 .widget .AppCompatEditText ;
2221import android .text .Editable ;
2322import android .text .Layout ;
2423import android .text .StaticLayout ;
@@ -57,6 +56,7 @@ public class MaterialEditText extends MaterialBaseEditText {
5756 public static final int FLOATING_LABEL_NONE = 0 ;
5857 public static final int FLOATING_LABEL_NORMAL = 1 ;
5958 public static final int FLOATING_LABEL_HIGHLIGHT = 2 ;
59+ public static final int FLOATING_LABEL_BOTTOM = 3 ;
6060
6161 final int DRAWABLE_LEFT = 0 ;
6262 final int DRAWABLE_TOP = 1 ;
@@ -92,6 +92,11 @@ public class MaterialEditText extends MaterialBaseEditText {
9292 */
9393 private int floatingLabelTextSize ;
9494
95+ /**
96+ * the floating label's text size.
97+ */
98+ private int floatingLabelMode ;
99+
95100 /**
96101 * the floating label's text color.
97102 */
@@ -387,7 +392,8 @@ private void init(Context context, AttributeSet attrs) {
387392 }
388393
389394 primaryColor = typedArray .getColor (R .styleable .MaterialEditText_met_primaryColor , defaultPrimaryColor );
390- setFloatingLabelInternal (typedArray .getInt (R .styleable .MaterialEditText_met_floatingLabel , 0 ));
395+ floatingLabelMode = typedArray .getInt (R .styleable .MaterialEditText_met_floatingLabel , 0 );
396+ setFloatingLabelInternal (floatingLabelMode );
391397 errorColor = typedArray .getColor (R .styleable .MaterialEditText_met_errorColor , Color .parseColor ("#e7492E" ));
392398 disabledUnderlineColor = typedArray .getColor (R .styleable .MaterialEditText_met_disabledBottomLineColor , -1 );
393399 minCharacters = typedArray .getInt (R .styleable .MaterialEditText_met_minCharacters , 0 );
@@ -828,11 +834,11 @@ private boolean adjustBottomLines() {
828834 }
829835 int destBottomLines ;
830836 textPaint .setTextSize (bottomTextSize );
831- if (tempErrorText != null || helperText != null ) {
832- Layout .Alignment alignment = (getGravity () & Gravity .RIGHT ) == Gravity .RIGHT || isRTL () ?
833- Layout .Alignment .ALIGN_OPPOSITE : (getGravity () & Gravity .LEFT ) == Gravity .LEFT ?
837+ if (getBottomText () != null ) {
838+ Layout .Alignment alignment = (getGravity () & Gravity .END ) == Gravity .END || isRTL () ?
839+ Layout .Alignment .ALIGN_OPPOSITE : (getGravity () & Gravity .START ) == Gravity .START ?
834840 Layout .Alignment .ALIGN_NORMAL : Layout .Alignment .ALIGN_CENTER ;
835- textLayout = new StaticLayout (tempErrorText != null ? tempErrorText : helperText , textPaint , getWidth () - getBottomTextLeftOffset () - getBottomTextRightOffset () - getPaddingLeft () - getPaddingRight (), alignment , 1.0f , 0.0f , true );
841+ textLayout = new StaticLayout (getBottomText () , textPaint , getWidth () - getBottomTextLeftOffset () - getBottomTextRightOffset () - getPaddingLeft () - getPaddingRight (), alignment , 1.0f , 0.0f , true );
836842 destBottomLines = Math .max (textLayout .getLineCount (), minBottomTextLines );
837843 } else {
838844 destBottomLines = minBottomLines ;
@@ -844,6 +850,19 @@ private boolean adjustBottomLines() {
844850 return true ;
845851 }
846852
853+ private CharSequence getBottomText () {
854+ if (tempErrorText != null )
855+ return tempErrorText ;
856+
857+ if (floatingLabelMode == FLOATING_LABEL_BOTTOM && getHint () != null )
858+ return getHint ();
859+
860+ if (helperText != null )
861+ return helperText ;
862+
863+ return null ;
864+ }
865+
847866 /**
848867 * get inner top padding, not the real paddingTop
849868 */
@@ -1355,8 +1374,8 @@ protected void onDraw(@NonNull Canvas canvas) {
13551374
13561375 // draw the bottom text
13571376 if (textLayout != null ) {
1358- if (tempErrorText != null || (( helperTextAlwaysShown || hasFocus ()) && ! TextUtils . isEmpty ( helperText ) )) { // error text or helper text
1359- textPaint .setColor (tempErrorText != null ? errorColor : helperTextColor != - 1 ? helperTextColor : ( baseColor & 0x00ffffff | 0x44000000 ));
1377+ if (shouldDrawBottomText ( )) { // error text or helper text
1378+ textPaint .setColor (getBottomTextColor ( ));
13601379 canvas .save ();
13611380 if (isRTL ()) {
13621381 canvas .translate (endX - textLayout .getWidth (), lineStartY + bottomSpacing - bottomTextPadding );
@@ -1417,6 +1436,39 @@ protected void onDraw(@NonNull Canvas canvas) {
14171436 super .onDraw (canvas );
14181437 }
14191438
1439+ private boolean shouldDrawBottomText () {
1440+
1441+ if (tempErrorText != null ) {
1442+ return true ;
1443+ }
1444+
1445+ if (((helperTextAlwaysShown || hasFocus ()) && !TextUtils .isEmpty (helperText ))) {
1446+ return true ;
1447+ }
1448+
1449+ if (floatingLabelMode == FLOATING_LABEL_BOTTOM && !TextUtils .isEmpty (getText ())) {
1450+ return true ;
1451+ }
1452+
1453+ return false ;
1454+ }
1455+
1456+ private Integer getBottomTextColor () {
1457+ if (tempErrorText != null ) {
1458+ return errorColor ;
1459+ }
1460+
1461+ if (floatingLabelMode == FLOATING_LABEL_BOTTOM ) {
1462+ return floatingLabelTextColor ;
1463+ }
1464+
1465+ if (helperTextColor != -1 ) {
1466+ return helperTextColor ;
1467+ } else {
1468+ return (baseColor & 0x00ffffff | 0x44000000 );
1469+ }
1470+ }
1471+
14201472 private void showClearButton () {
14211473 setCompoundDrawablesWithIntrinsicBounds (0 , 0 , clearButtonDrawable , 0 );
14221474 }
0 commit comments