【问题标题】:How to set floating text in EditText?如何在 EditText 中设置浮动文本?
【发布时间】:2017-11-22 22:32:14
【问题描述】:

在我的应用注册页面中,在此处编辑文本。在这个编辑文本中,我想显示浮动文本。但是我在下面的代码中尝试了这个,但没有用。请告诉任何人..

<?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 依赖项?
  • 我使用这个 android.support.design.widget.TextInputLayout 做到了。你到底有什么问题?您遇到了哪个错误?

标签: android xml android-edittext android-studio-2.3


【解决方案1】:
compile "com.android.support:design:$supportLibraryVersion"

在您的 build.gradle 文件中

【讨论】:

【解决方案2】:

我认为您错过了 gradle 文件依赖项,请检查设计依赖项。这对我来说是有效的。

 compile 'com.android.support:design:xx.x.x' 

【讨论】:

    【解决方案3】:

    在你的 build.gradle(module:app) 中添加这个依赖

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

    用您的支持库版本更改 25.2.0

    希望对你有帮助!

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多