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

时间:2015-05-29 19:15:52

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

我正在使用最近发布的Android Design Support Library来显示带有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>

我还试图为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());

15 个答案:

答案 0 :(得分:84)

<强>更新

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

原始答案:

如@shkschneider所述,这是一个known bug。 Github用户@ ljubisa987最近发布了一个Gist解决方法:

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

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

答案 1 :(得分:13)

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

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

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

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

答案 2 :(得分:6)

此问题已在22.2.1版中修复

答案 3 :(得分: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>

答案 4 :(得分: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更新为最新版本。 see output here

答案 5 :(得分:3)

此问题已在appcompat-v7支持设计库中得到解决。我还将23.0.1更新为compileSdkVersion,将23更新为buildToolsVersion&amp; build.gradle 中的23.0.1android { compileSdkVersion 23 buildToolsVersion "23.0.1" } dependencies { compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' }

// Clear all fragments and show base fragment, So it will not create
// issue after opening
// multiple apps
FrameLayout flSliding = (FrameLayout) findViewById(R.id.fl_sliding);
flSliding.post(new Runnable() {

    @Override
    public void run() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Log.i("NavigationDrawerActivity",
                "onCreate " + fragmentManager.getBackStackEntryCount());
        if (fragmentManager.getBackStackEntryCount() > 0) {
            removeFragment(fragmentManager.getBackStackEntryCount());
        }
        Log.i("NavigationDrawerActivity",
                "onCreate " + fragmentManager.getBackStackEntryCount());
    }
});

答案 6 :(得分: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()不同。如果您要在TextInputLayoutview animations上显示tranistions,则需要在animation的末尾执行上述代码或transition。只需确保上面的代码在用户界面thread上运行。

答案 7 :(得分:0)

在EditText上设置onFousListener时,看起来会出现此问题 - 尝试扩展EditText并支持多个onFocusListeners

答案 8 :(得分:0)

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

<item name="android:textColorHint">#FF424242</item>

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

答案 9 :(得分:0)

问题解决了: 转到你的应用程序的build.gradle并检查设计库。 编译&#39; com.android.support:设计:22.2.1&#39; 它应该是22.2.1或更新。

答案 10 :(得分:0)

我找到了不同的东西。

设置片段的样式时,

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

当我尝试不同的样式而不是&#34; Theme_DeviceDefault_Dialog&#34;时,错误消失了。

试一试。

答案 11 :(得分:0)

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

答案 12 :(得分:0)

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

<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 =&#34; @ style / Base.TextAppearance.AppCompat&#34; 添加到TextEditLayout即可,它可以正常工作,您可以根据需要更改主题。

答案 13 :(得分:0)

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

答案 14 :(得分:0)

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

 parent="Theme.MaterialComponents.Light.NoActionBar"