TextInputLayout 在用户关注它之前不显示 EditText 提示
TextInputLayout not showing EditText hint before user focus on it
我正在使用最近发布的 Android Design Support Library 来显示带有 EditText 的浮动标签。但我面临的问题是,当呈现 UI 时,EditText 上的提示未显示,但在我关注 EditText 后我看到了提示。
我的布局如下:
<?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());
你应该使用 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>
<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>
使用此代码 snippet.Make 确保您的 activity 是 AppCompatActivity。
和依赖也有最新版本
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
将 android 工作室更新到最新版本。
see output here
那是 Android 设计库的 known bug。已接受并分配。
因此您应该在下一个 Android 设计库版本中修复此问题。
与此同时,您可以查看问题跟踪器,寻找可能发布在那里的骇人听闻的修复程序,但我目前还不知道。
是的,它只影响 Lollipop 及更高版本。
更新:
这是一个错误,已在库的 22.2.1 版中修复。
原答案:
如@shkschneider 所述,这是 known bug。 Github 用户@ljubisa987 最近发布了一个解决方法的要点:
https://gist.github.com/ljubisa987/e33cd5597da07172c55d
如评论中所述,解决方法仅适用于 Android Lollipop 及更早版本。它不适用于 Android M 预览。
当您在 EditText 上设置 onFousListener 时似乎会出现此问题 - 尝试扩展 EditText 并支持多个 onFocusListeners
此问题已在版本 22.2.1 中修复
我用这段代码解决了我的问题:
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()
不同。如果你想在有 animations
或 tranistions
的 view
上显示 TextInputLayout
,你需要在 animation
的末尾执行上面的代码或 transition
。只需确保上面的代码在 UI thread
.
上运行
如果您以编程方式设置 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>
此问题已在支持设计库的 v23.0.1
中得到解决。我还在 build.gradle 中将我的 appcompat-v7
更新为 23.0.1
,将 compileSdkVersion
更新为 23
并将 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'
}
设置与 EditText 背景形成对比的提示颜色。
<item name="android:textColorHint">#FF424242</item>
相关:
setHintTextColor() in EditText
问题已解决:
转到您的应用程序的 build.gradle 并检查设计库。
编译 'com.android.support:design:22.2.1'
它应该是 22.2.1 或更新版本。
这在设计库 23.1.1 中对我有用:
在 TextInputLayout 的 xml 属性中设置提示颜色:
<android.support.design.widget.TextInputLayout
....
android:layout_margin="15dp"
android:textColorHint="@color/hintColor"
android:layout_width="match_parent"
...
我发现了一些不同之处。
为 fragment
、
设置样式时
f1.setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_DeviceDefault_Light);
当我尝试与 "Theme_DeviceDefault_Dialog"
不同的样式时,错误消失了。
试一试。
问题是提示颜色。键入内容时它会变成白色。请更改提示颜色或更改背景颜色。您将在输入时看到提示。
有一个简单的解决方案,因为这对我有用
<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,它应该可以工作,您可以更改随心所欲的主题。
只需为 TextInputLayout
添加:android:hint="your_hint"
。
如果您将布局背景设置为白色,请在您的 Activity 主题中使用它
parent="Theme.MaterialComponents.Light.NoActionBar"
我正在使用最近发布的 Android Design Support Library 来显示带有 EditText 的浮动标签。但我面临的问题是,当呈现 UI 时,EditText 上的提示未显示,但在我关注 EditText 后我看到了提示。
我的布局如下:
<?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());
你应该使用 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>
<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>
使用此代码 snippet.Make 确保您的 activity 是 AppCompatActivity。 和依赖也有最新版本
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
将 android 工作室更新到最新版本。 see output here
那是 Android 设计库的 known bug。已接受并分配。
因此您应该在下一个 Android 设计库版本中修复此问题。
与此同时,您可以查看问题跟踪器,寻找可能发布在那里的骇人听闻的修复程序,但我目前还不知道。
是的,它只影响 Lollipop 及更高版本。
更新:
这是一个错误,已在库的 22.2.1 版中修复。
原答案:
如@shkschneider 所述,这是 known bug。 Github 用户@ljubisa987 最近发布了一个解决方法的要点:
https://gist.github.com/ljubisa987/e33cd5597da07172c55d
如评论中所述,解决方法仅适用于 Android Lollipop 及更早版本。它不适用于 Android M 预览。
当您在 EditText 上设置 onFousListener 时似乎会出现此问题 - 尝试扩展 EditText 并支持多个 onFocusListeners
此问题已在版本 22.2.1 中修复
我用这段代码解决了我的问题:
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()
不同。如果你想在有 animations
或 tranistions
的 view
上显示 TextInputLayout
,你需要在 animation
的末尾执行上面的代码或 transition
。只需确保上面的代码在 UI thread
.
如果您以编程方式设置 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>
此问题已在支持设计库的 v23.0.1
中得到解决。我还在 build.gradle 中将我的 appcompat-v7
更新为 23.0.1
,将 compileSdkVersion
更新为 23
并将 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'
}
设置与 EditText 背景形成对比的提示颜色。
<item name="android:textColorHint">#FF424242</item>
相关:
setHintTextColor() in EditText
问题已解决: 转到您的应用程序的 build.gradle 并检查设计库。 编译 'com.android.support:design:22.2.1' 它应该是 22.2.1 或更新版本。
这在设计库 23.1.1 中对我有用:
在 TextInputLayout 的 xml 属性中设置提示颜色:
<android.support.design.widget.TextInputLayout
....
android:layout_margin="15dp"
android:textColorHint="@color/hintColor"
android:layout_width="match_parent"
...
我发现了一些不同之处。
为 fragment
、
f1.setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_DeviceDefault_Light);
当我尝试与 "Theme_DeviceDefault_Dialog"
不同的样式时,错误消失了。
试一试。
问题是提示颜色。键入内容时它会变成白色。请更改提示颜色或更改背景颜色。您将在输入时看到提示。
有一个简单的解决方案,因为这对我有用
<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,它应该可以工作,您可以更改随心所欲的主题。
只需为 TextInputLayout
添加:android:hint="your_hint"
。
如果您将布局背景设置为白色,请在您的 Activity 主题中使用它
parent="Theme.MaterialComponents.Light.NoActionBar"