如何在EditText中设置浮动文本?

How to set floating text in EditText?

在我的应用程序注册页面中,在此处编辑文本。在此编辑文本中,我想显示浮动文本。但我尝试了下面的代码但没有 working.Anybody 请告诉我..

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.citrin.popeye.dev.popeye.Registration">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/signupttl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="25dp"
        android:text="Register"
        android:textSize="24dp"
        android:layout_weight="1"
        android:textColor="#000000"/>
    <TextView
        android:id="@+id/signupttl1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="6dp"
        android:text="Sign up to create an account."
        android:textSize="15dp"
        android:layout_weight="1"/>

    <android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHint="#BDBDBD"
        android:layout_weight="1">
        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="38dp"
            android:drawablePadding="20dp"
            android:textSize="16dp"
            android:drawableLeft="@drawable/ic_name"
            android:inputType="textCapSentences"
            android:hint="Name"
            android:nextFocusDown="@+id/email"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHint="#BDBDBD"
        android:layout_weight="1">
        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:drawableLeft="@drawable/ic_email"
            android:drawablePadding="20dp"
            android:hint="Email ID"
            android:inputType="textEmailAddress"
            android:nextFocusDown="@+id/nation"
            android:textSize="16dp" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHint="#BDBDBD"
        android:layout_weight="1">
        <EditText
            android:id="@+id/nation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/ic_flag"
            android:layout_marginTop="16dp"
            android:drawablePadding="20dp"
            android:textSize="16dp"
            android:inputType="textCapWords"
            android:hint="Nationality"
            android:nextFocusDown="@+id/cor"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHint="#BDBDBD"
        android:layout_weight="1">
        <EditText
            android:id="@+id/cor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/ic_country"
            android:layout_marginTop="16dp"
            android:inputType="textCapWords"
            android:textSize="16dp"
            android:drawablePadding="20dp"
            android:hint="Country of Residence"
            android:nextFocusDown="@+id/pass"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHint="#BDBDBD"
        android:layout_weight="1">
        <EditText
            android:id="@+id/pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawablePadding="20dp"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/ic_pass"
            android:layout_marginTop="16dp"
            android:inputType="numberPassword"
            android:textSize="16dp"
            android:maxLength="4"
            android:hint="Digital Secure Key password"
            android:nextFocusDown="@+id/signup"/>
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/signup"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:text="SIGN UP"
        android:layout_weight="1"
        android:textColor="#FFFFFF"
        android:background="@drawable/btn"
        android:layout_marginBottom="15dp"
        android:onClick="onSignUpClick"/>

</LinearLayout>

</ScrollView>

下面附上截图

This is above code screenshot in android studio preview

我认为您错过了 gradle 文件依赖性,请检查设计依赖性。对我来说很管用。

 compile 'com.android.support:design:xx.x.x' 
compile "com.android.support:design:$supportLibraryVersion"

在您的 build.gradle 文件中

在您的 build.gradle(module:app)

中添加此依赖项

编译'com.android.support:design:25.2.0'

25.2.0 更改为您的支持库版本

希望对您有所帮助!

我认为你应该添加这两个依赖项

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'