【问题标题】:TextInputLayout not showing EditText hint before user focus on itTextInputLayout 在用户关注它之前不显示 EditText 提示
【发布时间】:2015-08-12 18:46:54
【问题描述】:

我正在使用最近发布的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>

我也尝试使用方法setHintTextInputLayout 设置提示,但没有运气。

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 的提示。
  • 你能检查你的提示颜色,或者尝试改变你的视图背景颜色,也许他们在那里,但因为颜色你看不到
  • @Ultimo_m 我认为提示颜色一定不是问题。但是我会在更改背景颜色后尝试看看。
  • @Shajeel Afzal 我告诉你这个建议是因为我和你有同样的问题,当我向视图添加背景时,我看到了提示。我在片段中使用它。我没有从java添加任何代码,我所做的只是来自xml
  • 我在添加/删除片段时看到了这个问题。但是如果我显示/隐藏片段,我看不到这个问题。

标签: android material-design android-design-library


【解决方案1】:

更新:

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

原答案:

正如@shkschneider 所提到的,这是一个known bug。 Github 用户 @ljubisa987 最近发布了 Gist 以寻求解决方法:

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

如 cmets 中所述,该解决方法仅适用于 Android Lollipop 和更早版本。它不适用于 Android M 预览版。

【讨论】:

  • Android MDC 是预览版。
  • 顺便说一句,错误状态现在似乎是“未来版本”。
  • 评论 #28:code.google.com/p/android/issues/… 为我工作
  • 在 22.2.1 版本中修复。
  • 我们确定这在 V22.2.1 中已修复吗?我在 Android 版本 5.1.1 上运行的设计库 v22.2.1 中遇到了这个问题。我在工具栏中有两个 EditText,我正在 Activity 过渡到,提示文本在聚焦之前不会显示。
【解决方案2】:

那是 Android 设计库的known bug。它已被接受分配

因此,您应该在下一个 Android 设计库版本中修复此问题。

与此同时,您可以查看问题跟踪器以获取可能会在此处发布的 hacky-fix,但我目前还不知道。

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

【讨论】:

  • 此问题已在 22.2.1 版本中修复
【解决方案3】:

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

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

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

【讨论】:

  • textColorHint 属性也为我修复了它。
【解决方案4】:

此问题已在版本 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>
    

    【讨论】:

      【解决方案6】:
         <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>
      

      使用此代码 sn-p。确保您的活动是 AppCompatActivity。 并且依赖也是最新版本的

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

      将 android studio 更新到最新版本。 see output here

      【讨论】:

        【解决方案7】:

        如果您以编程方式设置 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>
        

        【讨论】:

          【解决方案8】:

          此问题已在支持设计库的v23.0.1 中得到解决。我还在 build.gradle 中将appcompat-v7 更新为23.0.1compileSdkVersion 更新为23buildToolsVersion 更新为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??
          • 发生这种情况是因为“支持库”v23 需要 API23 来编译项目。所以主要关系是“支持库”和“compileSdkVersion”之间的关系。即使“targetSdkVersion”或“minSdkVersion”较低,您也应该始终匹配“compileSdkVersion”。原因很简单,库的版本反映了针对它构建的Android sdk的版本。
          【解决方案9】:

          我用这段代码解决了我的问题:

          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 上运行即可。

          【讨论】:

            【解决方案10】:

            设置与 EditText 背景形成对比的提示颜色。

            &lt;item name="android:textColorHint"&gt;#FF424242&lt;/item&gt;

            相关:
            setHintTextColor() in EditText
            Change EditText hint color when using TextInputLayout
            How to change the floating label color of TextInputLayout

            【讨论】:

              【解决方案11】:

              当您在 EditText 上设置 onFousListener 时似乎会出现此问题 - 尝试扩展 EditText 并支持多个 onFocusListeners

              【讨论】:

                【解决方案12】:

                问题已解决: 转到您的应用程序的 build.gradle 并检查设计库。 编译'com.android.support:design:22.2.1' 它应该是 22.2.1 或更高版本。

                【讨论】:

                  【解决方案13】:

                  问题是提示颜色。输入内容时它变成白色。请更改提示颜色或更改背景颜色。您将在输入时看到提示。

                  【讨论】:

                    【解决方案14】:

                    有一个简单的解决方案,因为这对我有用

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/activity_login_til_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="27dp"
                        android:layout_marginTop="24dp"
                        android:padding="10dp"
                        app:passwordToggleEnabled="true"
                        app:passwordToggleTint="#000000"
                        app:passwordToggleDrawable="@drawable/passwordviewtoggleselector"
                        android:theme="@style/Base.TextAppearance.AppCompat">
                          <!---- add this line and theme as you need ----->
                        <android.support.design.widget.TextInputEditText
                            android:id="@+id/activity_login_et_password"
                            android:layout_width="match_parent"
                            android:layout_height="58dp"
                            android:hint="Password"
                            android:inputType="textPassword"
                            android:padding="10dp"
                            android:textColor="#f5ab3a"
                            android:textColorHint="#e6d2d1cf"
                            android:textSize="20dp" />
                    
                    </android.support.design.widget.TextInputLayout>
                    

                    只需将 android:theme="@style/Base.TextAppearance.AppCompat" 添加到 TextEditLayout 即可,您可以根据需要更改主题。

                    【讨论】:

                      【解决方案15】:

                      只需为TextInputLayout 添加:android:hint="your_hint"

                      【讨论】:

                        【解决方案16】:

                        我发现了一些不同的东西。

                        在为fragment设置样式时,

                        f1.setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_DeviceDefault_Light);
                        

                        当我尝试与"Theme_DeviceDefault_Dialog" 不同的样式时,该错误消失了。

                        试一试。

                        【讨论】:

                          【解决方案17】:

                          如果您已将布局背景设置为白色,请在您的活动主题中使用它

                           parent="Theme.MaterialComponents.Light.NoActionBar"
                          

                          【讨论】:

                            猜你喜欢
                            • 2018-07-22
                            • 2011-04-06
                            • 1970-01-01
                            • 2015-08-29
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多