如何在android EditText字段中保留提示文本?
在我的android应用程序中,我的EditText视图中有一个提示文本。一旦用户开始键入,我希望提示显示在edittext字段的顶部。用户开始键入时,原始提示文本消失,这很好。如何将文本放置在edittext字段的顶部?有没有办法使EditText字段多行并使用第一行作为提示?但是用户输入应该总是从第二行开始 提前感谢您的帮助如何在android EditText字段中保留提示文本?,android,android-layout,android-edittext,Android,Android Layout,Android Edittext,在我的android应用程序中,我的EditText视图中有一个提示文本。一旦用户开始键入,我希望提示显示在edittext字段的顶部。用户开始键入时,原始提示文本消失,这很好。如何将文本放置在edittext字段的顶部?有没有办法使EditText字段多行并使用第一行作为提示?但是用户输入应该总是从第二行开始 提前感谢您的帮助 刚刚发现它们在android中被称为浮动内联标签。 但API 21似乎还不支持这一点 您可以使用。它是一个自定义组件,按照google文档中的描述工作 创建自定义组件
- 刚刚发现它们在android中被称为浮动内联标签。
但API 21似乎还不支持这一点
- 您可以使用。它是一个自定义组件,按照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>
在上面添加一个文本视图,并将其设置为“消失”或“不可见”。当用户开始键入时,将tv visibility设置为Visible键入文本时,此答案有问题,提示消失。我使用的是26.0.2