带提示的 EditText,提示移动到哪里

EditText with hint, where hint moves

我看到 EditText 有一个提示,当用户点击它时,提示会向上移动,减小字体大小并使用户可以编辑该字段。当用户移动到另一个 EditText 并将其留空时,提示会以完整尺寸显示。

它是什么以及如何使用 XML 在 Android Studio 上的 activity 中添加它?

我正在 Android Studio 使用 Kotlin。

如果你是这个意思

TextInputLayout

build.gradle(模块:app)文件中添加设计支持库的依赖项,如下所示。

implementation 'com.google.android.material:material:<version>'

目前最新版本是1.2.0-alpha03你可以从mvnrepository

看到最新版本

然后,您可以在 xml 布局中像这样使用它。

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/myTextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/my_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/YOUR_HINT"
        android:singleLine="true"
        android:textColor="@color/colorDarkestGray" />
</com.google.android.material.textfield.TextInputLayout>

这是 TextInputLayout,您可以像这样将 editText 用作 child:

首先build.gradle文件中添加设计支持库的依赖,如下所示。

implementation 'com.android.support:design:25.3.1'

如果您使用的是 AndroidX,请使用:

implementation 'com.google.android.material:material:1.0.0'

第二个,然后这样实现:

   <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/activity_horizontal_margin">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Floating Hint Enabled Default" />

    </android.support.design.widget.TextInputLayout>

Android TextInputLayout 特点

1-Enabling/Disabling floating hints

2-Enabling/Disabling floating hint animation

3-Displaying Error Messages

4-Showing Character Counter

5-Password Visibility Toggle and . . .