Disable/Remove floating label hint text in TextInputLayout XML

前端 未结 11 1330
一向
一向 2021-01-31 06:59

This may seem counter-intuitive but is there a way to disable or remove the floating label hint in TextInputLayout? The reason I want to use TextInputLayout

相关标签:
11条回答
  • 2021-01-31 07:20

    Starting version 23.2.0 of the Support Library you can call

    setHintEnabled(false)
    

    or putting it in your TextInputLayout xml as such :

    app:hintEnabled="false"
    

    Though the name might makes you think it removes all hints, it just removes the floating one.

    Related docs and issue: http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setHintEnabled(boolean)

    https://code.google.com/p/android/issues/detail?id=181590

    0 讨论(0)
  • 2021-01-31 07:23

    There may be three ways to go about achieving this:

    1 Set android:hint on TextInputLayout to a space _ character, and keep android:hint="This is my cool hint" set on the EditText.

    <android.support.design.widget.TextInputLayout
        ....
        ....
        android:hint=" ">       <<----------
    
        <EditText
            ....
            ....
            android:hint="This is my cool hint"/>    <<----------
    
    </android.support.design.widget.TextInputLayout>
    

    This works because TextInputLayout performs the following check before using the EditText's hint:

    // If we do not have a valid hint, try and retrieve it from the EditText
    if (TextUtils.isEmpty(mHint)) {
        setHint(mEditText.getHint());
        // Clear the EditText's hint as we will display it ourselves
        mEditText.setHint(null);
    }
    

    By setting android:hint=" ", if (TextUtils.isEmpty(mHint)) evaluates to false, and the EditText retains its hint.

    2 Second option would be to subclass TextInputLayout and override its addView(View child, int index, ViewGroup.LayoutParams params) method:

    public class CTextInputLayout extends TextInputLayout {
    
        public CTextInputLayout(Context context) {
            this(context, null);
        }
    
        public CTextInputLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void addView(View child, int index, ViewGroup.LayoutParams params) {
            if (child instanceof EditText) {
                // cache the actual hint
                CharSequence hint = ((EditText)child).getHint();
                // remove the hint for now - we don't want TextInputLayout to see it
                ((EditText)child).setHint(null);
                // let `TextInputLayout` do its thing
                super.addView(child, index, params);
                // finally, set the hint back
                ((EditText)child).setHint(hint);
            } else {
                // Carry on adding the View...
                super.addView(child, index, params);
            }
        }
    }
    

    Then use your custom CTextInoutLayout instead of the one from the design support library:

    <your.package.name.CTextInputLayout
        ....
        .... >       <<----------
    
        <EditText
            ....
            ....
            android:hint="This is my cool hint"/>    <<----------
    
    </your.package.name.CTextInputLayout>
    

    3 Third, and probably the most straight-forward way would be to make the following calls:

    // remove hint from `TextInputLayout`
    ((TextInputLayout)findViewById(R.id.textContainer)).setHint(null);
    // set the hint back on the `EditText`
    // The passed `String` could also be a string resource
    ((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt.");
    
    0 讨论(0)
  • 2021-01-31 07:24

    if you have used app:hintEnabled="false" then also set android:paddingTop="8dp" in EditText and boom top hint space is gone, this worked for me hope this works for you too

    0 讨论(0)
  • 2021-01-31 07:28

    If someone have problems like me:

    The label make the text padding show wrong then do this:

    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:paddingTop="@dimen/default_padding"
                        android:paddingBottom="@dimen/default_padding"
                        android:layout_height="wrap_content"
                        android:hint="@string/search_hint" />
    

    Add padding top and padding bottom to the TextInputEditText that will fix the problem

    0 讨论(0)
  • 2021-01-31 07:33

    I've tried all of answers, but non of them are working now (specifically for com.google.android.material:material:1.1.0-beta01). Even if we make changes to EditText addition logic in TextInputLayout, we have empty space on top of field that blanking half of text and hint. Now I have a solution for the problem. Main thing is a padding in EditText, as mentioned in material.io:

    <com.google.android.material.textfield.TextInputLayout
                 . . .
        android:layout_height="wrap_content"
        app:hintEnabled="false"
        app:startIconDrawable="@drawable/ic_search_black"
        app:endIconMode="clear_text">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="12dp"
            android:hint="@string/showcase_search_hint_text"
            android:inputType="text" />
    
    </com.google.android.material.textfield.TextInputLayout>
    

    It allows us to implement a 'SearchView'-like view with search icon and text deletion button without any ugly "magic" with custom views

    0 讨论(0)
  • 2021-01-31 07:38
    myEditText.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) textContainer.hint = null
                else myEditText.hint = getString(R.string.your_string)
            }
    

    it makes your hint gone perfectly in your textInputLayout, because if you want to make it gone with app:hintEnabled="false" that's make your textInputLayout not cool :)

    0 讨论(0)
提交回复
热议问题