TextInputLayout在用户关注它之前没有显示EditText提示

Sha*_*zal 94 android material-design android-design-library

我正在使用最近发布的Android设计支持库来显示带有EditTexts的浮动标签.但我面临的问题是,在渲染UI时,EditText上的提示没有显示,但是在我专注于EditTexts之后我看到了Hint.

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/name_et_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin">

                <EditText
                    android:id="@+id/FeedBackerNameET"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackname"
                    android:inputType="textPersonName|textCapWords" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/email_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/FeedBackerEmailET"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackemail"
                    android:inputType="textEmailAddress" />

            </android.support.design.widget.TextInputLayout>

            <Spinner
                android:id="@+id/SpinnerFeedbackType"
                android:layout_width="fill_parent"
                android:layout_height="48dp"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:entries="@array/feedbacktypelist"
                android:prompt="@string/feedbacktype" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/body_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/EditTextFeedbackBody"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackbody"
                    android:inputType="textMultiLine|textCapSentences"
                    android:lines="5" />

            </android.support.design.widget.TextInputLayout>

            <CheckBox
                android:id="@+id/CheckBoxFeedBackResponse"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:text="@string/feedbackresponse" />

            <Button
                android:id="@+id/ButtonSendFeedback"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:text="@string/feedbackbutton" />
        </LinearLayout>
    </ScrollView>

</FrameLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:background="@drawable/toolbar_shadow" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我也尝试设置TextInputLayout使用方法的提示,setHint但没有运气.

mNameTextInputLayout = (TextInputLayout) v.findViewById(R.id.name_et_textinputlayout);
mNameTextInputLayout.setErrorEnabled(true);
mNameTextInputLayout.setHint(feedBackerNameET.getHint());

mEmailTextInputLayout = (TextInputLayout) v.findViewById(R.id.email_textinputlayout);
mEmailTextInputLayout.setErrorEnabled(true);
mEmailTextInputLayout.setHint(feedBackerEmail.getHint());

mBodyTextInputLayout = (TextInputLayout) v.findViewById(R.id.body_textinputlayout);
mBodyTextInputLayout.setErrorEnabled(true);
mBodyTextInputLayout.setHint(feedBackBody.getHint());
Run Code Online (Sandbox Code Playgroud)

Dav*_*sen 84

更新:

这是一个已在库22.2.1版中修复的错误.

原答案:

正如@shkschneider所提到的,这是一个已知的错误.Github用户@ ljubisa987最近发布了一个Gist的解决方法:

https://gist.github.com/ljubisa987/e33cd5597da07172c55d

如评论中所述,该解决方法仅适用于Android Lollipop及更早版本.它不适用于Android M预览版.

  • 这在Android M上不起作用 (4认同)
  • 我们是否确定在V22.2.1中已修复此问题?我在Android 5.1.1版上运行的设计库v22.2.1中遇到了这个问题.我在工具栏中有两个EditText,我正在进行活动转换,并且提示文本在聚焦之前不会显示. (3认同)
  • 评论#28:https://code.google.com/p/android/issues/detail?id = 175228&q = TextInputLayout&solpec = ID%20Type%20Status%20Owner%20Summary%20Stars for me for me (2认同)

shk*_*der 13

这是Android设计库的一个已知错误.它已被接受分配.

所以你应该在下一个Android设计库版本中修复它.

在此期间,您可以观看问题跟踪器以获得可能会在那里发布的hacky-fix,但我现在还不知道.

是的,它只影响棒棒糖及以上.


Emb*_*ous 11

这在设计库23.1.1中适用于我:

在TextInputLayout的xml属性中设置提示颜色:

    <android.support.design.widget.TextInputLayout
        ....
        android:layout_margin="15dp"
        android:textColorHint="@color/hintColor"
        android:layout_width="match_parent"
        ...
Run Code Online (Sandbox Code Playgroud)


Jdr*_*uwe 6

此问题已在版本22.2.1中修复


小智 5

你应该使用android.support.v7.widget.AppCompatEditTextas EditText并设置android:hint如下

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.AppCompatEditText
                android:id="@+id/etx_first_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"                    
                android:inputType="text"
                android:hint="@string/hint_first_name"/>
    </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)