Android:TextInputLayout的简单使用

        今天看了一下同事新写的登录注册界面,发现点击输入框之后EditText中的hint有浮动动画。于是看了一下实现的代码,发现使用了TextInputLayout这个东东。

1.效果

一般我们在EditText中写的hint会对当前EditText输入内容进行提示,一旦输入内容之后该EditText中的hint就会消失。这样的效果不算很好。
但是使用了TextInputLayout的EditText的话就不同了,使用了TextInputLayout之后,EditText中的内容依然还是会在EditText中提示当前输入框应该输入什么内容,但是当用户输入内容之后hint并不会消失,而是会变成另外一种颜色浮动到EditText上面。


2.用法

使用方法很简单,首先导入支持库:
在项目的build.gradle中添加依赖:
 compile ‘com.android.support:design:22.2.0‘
然后在EditText外表包一层TextInPutLayout就可以了。那么现在,当点击有hint的EditText的时候hint就会浮动起来。

3.其它

除了动画效果之外TextInputLayout还可以进行错误提示。
setErrorEnabled(true);
setError("请检查格式");
如果当前输入内容通不过所指定的格式,则可以调用以上两句代码,使得hint变色并提示错误。
当前输入内容符合所指定的格式之后,就可以diaoyongsetErrorEnabled(false);
恢复原貌










 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化此布局<com.google.android.material.textfield.TextInputLayout android:id="@+id/usernameLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="用户名" layout_height="" layout_width="" xmlns:android="http://schemas.android.com/apk/res/android"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:maxLines="1" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/passwordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="密码"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:maxLines="1" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/phoneLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="密码"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/phoneEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:maxLines="1" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/registerButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="注册" /> </com.google.android.material.textfield.TextInputLayout>
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值