禁用/删除TextInputLayout XML中的浮动标签提示文本

Mic*_*cro 45 android android-textinputlayout

这可能看起来违反直觉,但有没有办法禁用或删除浮动标签提示TextInputLayout?我想要使​​用的原因TextInputLayout而不仅仅是EditText用于TextInputLayout提供的计数器.

这是我到目前为止:

<android.support.design.widget.TextInputLayout
            android:id="@+id/textContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:counterEnabled="true"
            app:counterMaxLength="100">

            <EditText
                android:id="@+id/myEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:gravity="top|left"
                android:inputType="textMultiLine|textCapSentences"
                android:maxLength="100"
                android:scrollbars="vertical"
                android:hint="This is my cool hint"/>

        </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

Gau*_*ier 117

从支持库的23.2.0版开始,您可以调用

setHintEnabled(false)
Run Code Online (Sandbox Code Playgroud)

或者将它放在TextInputLayout xml中:

app:hintEnabled="false"
Run Code Online (Sandbox Code Playgroud)

虽然名称可能会让您认为它删除了所有提示,但它只是移除了浮动的提示.

相关文档和问题:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setHintEnabled(boolean)

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

  • 但当你设置 app:hintEnabled="false" 时,它会在顶部添加一些奇怪的填充:( 如果你在右侧使用图标,它会很明显。我已经将 paddingTop="12dp" 添加到 TextInputEditText,看起来不错。 (7认同)
  • 附加说明:为了使它起作用,您需要在`TextInputEditText`上设置提示。如果您在“ TextInputLayout”上设置提示,则不会显示。 (3认同)

Vik*_*ram 33

实现这一目标可能有三种方法:

1设置android:hintTextInputLayout,以一个空格_字符,并始终保持android:hint="This is my cool hint"在所设定的EditText.

<android.support.design.widget.TextInputLayout
    ....
    ....
    android:hint=" ">       <<----------

    <EditText
        ....
        ....
        android:hint="This is my cool hint"/>    <<----------

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为TextInputLayout在使用EditText's提示之前执行以下检查:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

通过设置android:hint=" ",if (TextUtils.isEmpty(mHint))评估falseEditText保留其提示.

2第二个选项是子类化TextInputLayout并覆盖其addView(View child, int index, ViewGroup.LayoutParams params)方法:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用您的自定义CTextInoutLayout而不是设计支持库中的自定义:

<your.package.name.CTextInputLayout
    ....
    .... >       <<----------

    <EditText
        ....
        ....
        android:hint="This is my cool hint"/>    <<----------

</your.package.name.CTextInputLayout>
Run Code Online (Sandbox Code Playgroud)

3第三,可能最直接的方式是进行以下调用:

// 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.");
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是另一个问题的解决方案。设置app:hintEnabled =“ false”时,您会得到奇怪的错误-框的顶部或底部(带有OutlinedBox样式)被剪切甚至丢失。CTextInputLayout是此错误的完美解决方案。(我正在使用材料:1.0.0,并在材料:1.1.0 alfa 1和2上进行了测试) (2认同)

小智 8

如果您使用了 app:hintEnabled="false" 然后还在 EditText 中设置 android:paddingTop="8dp" 并且繁荣顶部提示空间消失了,这对我有用希望这对你也有用


Cam*_*ino 6

使用com.google.android.material可以隐藏

<com.google.android.material.textfield.TextInputLayout

               ....

               app:hintAnimationEnabled="false"
               app:hintEnabled="false"
               >

                <com.google.android.material.textfield.TextInputEditText
                    ........
                    android:hint="@string/label_hint"
                    />

</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)


Rus*_*zov 5

我已经尝试了所有答案,但现在都没有工作(特别是 com.google.android.material:material:1.1.0-beta01)。即使我们对 TextInputLayout 中的 EditText 添加逻辑进行了更改,我们在字段顶部仍有空白区域,空白的文本和提示的一半。现在我有了解决问题的办法。最主要的是在EditText上的填充,如提及在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>
Run Code Online (Sandbox Code Playgroud)

它允许我们使用搜索图标和文本删除按钮实现类似“SearchView”的视图,而没有任何带有自定义视图的丑陋“魔法”