mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
[TextFields] Added expandedHintEnabled attribute (true by default). If set to false it allows for the hint to always be in its collapsed state.
If false it also allows for the placeholder text and prefix/suffix to be displayed when the text field is empty. Resolves https://github.com/material-components/material-components-android/issues/1459 Resolves https://github.com/material-components/material-components-android/issues/1644 Resolves https://github.com/material-components/material-components-android/pull/1645 Resolves https://github.com/material-components/material-components-android/pull/1163 PiperOrigin-RevId: 331181181
This commit is contained in:
parent
7d3cb73ca7
commit
22973ff103
@ -425,12 +425,14 @@ be set on the `TextInputEditText`)._
|
||||
|
||||
#### Label attributes
|
||||
|
||||
| Attribute | Related method(s) | Default value
|
||||
------------------------------ | ------------------------ | ------------------------------------------------------- | -------------
|
||||
**Text** | `android:hint` | `setHint`<br/>`getHint` | `null`
|
||||
**Color** | `android:textColorHint` | `setDefaultHintTextColor`<br/>`getDefaultHintTextColor` | `?attr/colorOnSurface` at 60% opacity (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml))
|
||||
**Collapsed (floating) color** | `app:hintTextColor` | `setHintTextColor`<br/>`getHintTextColor` | `?attr/colorPrimary`
|
||||
**Typography** | `app:hintTextAppearance` | `setHintTextAppearance` | `?attr/textAppearanceCaption`
|
||||
| Attribute | Related method(s) | Default value
|
||||
------------------------------ | -------------------------- | ------------------------------------------------------- | -------------
|
||||
**Text** | `android:hint` | `setHint`<br/>`getHint` | `null`
|
||||
**Color** | `android:textColorHint` | `setDefaultHintTextColor`<br/>`getDefaultHintTextColor` | `?attr/colorOnSurface` at 60% opacity (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml))
|
||||
**Collapsed (floating) color** | `app:hintTextColor` | `setHintTextColor`<br/>`getHintTextColor` | `?attr/colorPrimary`
|
||||
**Typography** | `app:hintTextAppearance` | `setHintTextAppearance` | `?attr/textAppearanceCaption`
|
||||
**Animation** | `app:hintAnimationEnabled` | `setHintAnimationEnabled`<br/>`isHintAnimationEnabled` | `true`
|
||||
**Expanded enabled** | `app:expandedHintEnabled` | `setExpandedHintEnabled`<br/>`isExpandedHintEnabled` | `true`
|
||||
|
||||
**Note:** The `android:hint` should always be set on the `TextInputLayout`
|
||||
instead of on the `EditText` in order to avoid unintended behaviors.
|
||||
|
||||
@ -406,6 +406,7 @@ public class TextInputLayout extends LinearLayout {
|
||||
|
||||
final CollapsingTextHelper collapsingTextHelper = new CollapsingTextHelper(this);
|
||||
|
||||
private boolean expandedHintEnabled;
|
||||
private boolean hintAnimationEnabled;
|
||||
private ValueAnimator animator;
|
||||
|
||||
@ -474,6 +475,7 @@ public class TextInputLayout extends LinearLayout {
|
||||
hintEnabled = a.getBoolean(R.styleable.TextInputLayout_hintEnabled, true);
|
||||
setHint(a.getText(R.styleable.TextInputLayout_android_hint));
|
||||
hintAnimationEnabled = a.getBoolean(R.styleable.TextInputLayout_hintAnimationEnabled, true);
|
||||
expandedHintEnabled = a.getBoolean(R.styleable.TextInputLayout_expandedHintEnabled, true);
|
||||
|
||||
shapeAppearanceModel =
|
||||
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, DEF_STYLE_RES).build();
|
||||
@ -1516,7 +1518,7 @@ public class TextInputLayout extends LinearLayout {
|
||||
collapsingTextHelper.setCollapsedTextColor(focusedTextColor);
|
||||
} // If none of these states apply, leave the expanded and collapsed colors as they are.
|
||||
|
||||
if (hasText || (isEnabled() && hasFocus)) {
|
||||
if (hasText || !expandedHintEnabled || (isEnabled() && hasFocus)) {
|
||||
// We should be showing the label so do so if it isn't already
|
||||
if (force || hintExpanded) {
|
||||
collapseHint(animate);
|
||||
@ -2877,6 +2879,31 @@ public class TextInputLayout extends LinearLayout {
|
||||
hintAnimationEnabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the hint expands to occupy the input area when the text field is unpopulated
|
||||
* and not focused.
|
||||
*
|
||||
* @see #setExpandedHintEnabled(boolean)
|
||||
* @attr ref com.google.android.material.R.styleable#TextInputLayout_hintExpadedEnabled
|
||||
*/
|
||||
public boolean isExpandedHintEnabled() {
|
||||
return expandedHintEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the hint should expand to occupy the input area when the text field is
|
||||
* unpopulated and not focused.
|
||||
*
|
||||
* @see #isExpandedHintEnabled()
|
||||
* @attr ref com.google.android.material.R.styleable#TextInputLayout_hintExpadedEnabled
|
||||
*/
|
||||
public void setExpandedHintEnabled(boolean enabled) {
|
||||
if (expandedHintEnabled != enabled) {
|
||||
expandedHintEnabled = enabled;
|
||||
updateLabelState(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
|
||||
@ -62,6 +62,7 @@
|
||||
<public name="helperTextTextAppearance" type="attr"/>
|
||||
<public name="helperTextTextColor" type="attr"/>
|
||||
<public name="hintAnimationEnabled" type="attr"/>
|
||||
<public name="expandedHintEnabled" type="attr"/>
|
||||
<public name="hintEnabled" type="attr"/>
|
||||
<public name="hintTextAppearance" type="attr"/>
|
||||
<public name="hintTextColor" type="attr"/>
|
||||
|
||||
@ -37,6 +37,9 @@
|
||||
<!-- Text color of the hint in the collapsed floating label.
|
||||
If set, this takes precedence over hintTextAppearance. -->
|
||||
<attr name="hintTextColor" format="color"/>
|
||||
<!-- Whether the hint should occupy the input area when the text field is
|
||||
unpopulated and not focused. -->
|
||||
<attr name="expandedHintEnabled" format="boolean"/>
|
||||
|
||||
<!-- The text to display as helper text underneath the text input area. -->
|
||||
<attr name="helperText" format="string"/>
|
||||
|
||||
@ -827,4 +827,26 @@ public class TextInputLayoutActions {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Sets whether the hint expands. */
|
||||
public static ViewAction setExpandedHintEnabled(final boolean expandedHintEnabled) {
|
||||
return new ViewAction() {
|
||||
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return ViewMatchers.isAssignableFrom(TextInputLayout.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Sets whether the hint expands.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
TextInputLayout layout = (TextInputLayout) view;
|
||||
layout.setExpandedHintEnabled(expandedHintEnabled);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +41,7 @@ import static com.google.android.material.testutils.TextInputLayoutActions.setEr
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setErrorContentDescription;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setErrorEnabled;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setErrorTextAppearance;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setExpandedHintEnabled;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setHelperText;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setHelperTextEnabled;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setHint;
|
||||
@ -176,6 +177,15 @@ public class TextInputLayoutTest {
|
||||
onView(withId(R.id.textinput_box_outline)).check(isCutoutOpen(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHintIsCollapsedIfExpandedHintNotEnabled() {
|
||||
// Set expandedHintEnabled to false.
|
||||
onView(withId(R.id.textinput_box_filled)).perform(setExpandedHintEnabled(false));
|
||||
|
||||
// Check the hint is collapsed.
|
||||
onView(withId(R.id.textinput_box_filled)).check(isHintExpanded(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetErrorEnablesErrorIsDisplayed() {
|
||||
onView(withId(R.id.textinput)).perform(setError(ERROR_MESSAGE_1));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user