如何保留 android EditText 字段中的提示文本?
How to retain the hint text in android EditText field?
在我的 android 应用程序中,我的 EditText 视图中有一个提示文本。一旦用户开始输入,我希望提示显示在 edittext 字段的顶部。当用户开始输入时,原始提示文本会消失,这很好。如何将文本放在 edittext 字段的顶部?有没有办法使 EditText 字段多行并使用第一行作为提示?但是用户输入应该总是从第二行开始。
在此先感谢您的帮助!
- 刚刚发现它们在android中被称为浮动内联标签。
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
但是看起来 API 21 还不支持这个。
您可以使用 FloatLabelLayout。它是一个自定义组件,按照 google 文档中的描述工作。
创建自定义组件后,您可以通过以下方式使用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>
android 提供的新小部件称为 TextInputLayout,这是标准方式。下面是一个例子:
<android.support.design.widget.TextInputLayout
android:id="@+id/first_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hello"/>
</android.support.design.widget.TextInputLayout>
在我的 android 应用程序中,我的 EditText 视图中有一个提示文本。一旦用户开始输入,我希望提示显示在 edittext 字段的顶部。当用户开始输入时,原始提示文本会消失,这很好。如何将文本放在 edittext 字段的顶部?有没有办法使 EditText 字段多行并使用第一行作为提示?但是用户输入应该总是从第二行开始。
在此先感谢您的帮助!
- 刚刚发现它们在android中被称为浮动内联标签。
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels 但是看起来 API 21 还不支持这个。
您可以使用 FloatLabelLayout。它是一个自定义组件,按照 google 文档中的描述工作。
创建自定义组件后,您可以通过以下方式使用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>
android 提供的新小部件称为 TextInputLayout,这是标准方式。下面是一个例子:
<android.support.design.widget.TextInputLayout
android:id="@+id/first_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hello"/>
</android.support.design.widget.TextInputLayout>