用户未聚焦时TextInputLayout不显示EditText提示

100

我最近使用了发布不久的Android Design Support Library来展示带有EditTexts的浮动标签。但我遇到一个问题,即当UI被渲染时,EditText上的提示信息没有显示出来,只有在我聚焦于EditTexts后才能看到提示信息。

我的布局如下:

<?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>

我也尝试使用 setHint 方法来为 TextInputLayout 设置提示,但没有成功。

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());

我遇到了同样的问题。令我惊讶的是,在4.1模拟器上运行应用程序工作得很好,但在我的M预览启用的Nexus上,TextInputLayout表现不佳,并且未显示嵌入EditText的提示。 - sashomasho
你能检查一下提示颜色,或者尝试更改视图背景颜色吗?也许它们在那里,只是因为颜色的缘故你看不到。 - Ultimo_m
@Ultimo_m 我认为提示颜色不应该是问题。不过在改变背景颜色后,我会尝试看一下。 - Shajeel Afzal
1
@Shajeel Afzal,我给你提供这个建议是因为我曾经遇到过和你一样的问题,当我在视图中添加背景时,我看到了提示。我是在一个片段中使用它的。我没有从Java中添加任何代码,我所做的都是在xml中完成的。 - Ultimo_m
2
当我添加/删除片段时,我遇到了这个问题。但如果我显示/隐藏片段,我就不会遇到这个问题。 - Dave Jensen
使用片段时出现相同的问题,如果我使用add事务,在返回时运行良好。如果我使用replace事务,在返回时您看不到提示。 - Ultimo_m
17个回答

84

更新:

这是该库的22.2.1版本中已经修复的错误。

原始答案:

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

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

正如评论中所指出的,此解决方法仅适用于Android Lollipop及更早版本。它在Android M预览版上不起作用。


1
Android MDC是一个预览版。 - Dave Jensen
1
顺便提一下,看起来现在的错误状态是“未来发布”。 - Dave Jensen
3
在V22.2.1版本中,这个问题是否已经得到解决?我在Android 5.1.1系统上运行的设计库V22.2.1中遇到了这个问题。我在一个工具栏中有两个EditText,在Activity转换时提示文本只有在输入焦点下才会显示出来。 - StuStirling
1
解决方案在我的三星Lollipop设备上无法运行。 - Johann
显示剩余3条评论

13
这是Android设计库的已知问题。它已被接受和分配。
因此,您应该在下一个Android设计库版本中修复此问题。
同时,您可以关注问题跟踪器,以获取可能发布的hacky-fix,但我目前不知道任何方法。
是的,它只影响棒棒糖及以上版本。

1
此问题已在22.2.1版本中修复。 - Sam

13

在Design Library 23.1.1中,以下内容适用于我:

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

    <android.support.design.widget.TextInputLayout
        ....
        android:layout_margin="15dp"
        android:textColorHint="@color/hintColor"
        android:layout_width="match_parent"
        ...

textColorHint属性对我也有帮助。 - JoM

6

该问题已在22.2.1版本中解决。


5

您应该使用 android.support.v7.widget.AppCompatEditText 作为 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>

3
   <android.support.design.widget.TextInputLayout
            android:id="@+id/editText1"
            android:layout_margin="15dp"
            android:layout_centerVertical="true"
            android:layout_width="match_parent"

            android:layout_height="wrap_content">

              <AutoCompleteTextView
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:singleLine="true"
                android:hint="Add City" />

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

使用此代码片段。确保您的活动是AppCompatActivity。并且依赖项也具有最新版本

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

将Android Studio升级到最新版本。 查看输出结果


3
如果您想在代码中设置EditText的提示文本,它是不起作用的!您需要在TextInputLayout上设置提示文本。
TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.usernameTextInputLayout);
textInputLayout.setHint(getString(R.string.username_hint));

以下是XML代码:

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

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/usernameEt"
                android:layout_width="match_parent"
                android:layout_height="@dimen/default_height"
                android:layout_marginEnd="@dimen/default_margin"
                android:layout_marginStart="@dimen/default_margin"
                android:maxLength="@integer/username"
                tools:hint="Username" />

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

3

这个问题在支持设计库的v23.0.1中已经解决。我还更新了我的appcompat-v723.0.1,将compileSdkVersion升级到23,并在build.gradle中将buildToolsVersion更新为23.0.1

android {    
compileSdkVersion 23
buildToolsVersion "23.0.1"
}

dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}

当您更改支持设计版本时,是否需要更改compleSdkVersion、buildtools版本和appCompatVersion? - srinivas
这是因为“支持库”v23需要API23来编译项目。因此,“支持库”和“compileSdkVersion”的主要关系是相互关联的。即使“targetSdkVersion”或“minSdkVersion”较低,您也应始终匹配“compileSdkVersion”。原因很简单,库的版本反映了其构建所依赖的Android SDK版本。 - Wahib Ul Haq

2
我用以下代码解决了我的问题:
new Handler().postDelayed(
        new Runnable() {
            @Override
            public void run() {
                TextInputLayout til = (TextInputLayout) someParentView.findViewById(R.id.til);
                til.setHint("Your Hint Text");
                til.bringToFront();
            }
        }, 10);

关键在于 bringToFront 方法。该方法能强制 TextInputLayout 重新绘制,而这与执行 invalidate() 不同。如果您试图在带有animationstranistionsview上显示TextInputLayout,则需要在animationtransition结束时执行上述代码。确保上述代码在 UI thread 上运行即可。

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接