Android 5.x新增控件之-TextInputLayout控件

本文系转载,原文地址http://blog.csdn.net/caihongdao123/article/details/51579366

hello,大家好!
今天给大家更新的博客是有关TextInputLayout的简单介绍。


TextInputLayout简介

TextInputLayout是把EditText作为自己子控件的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方。它解决了当用户点击EditText时Hint的文字消失了而导致用户可能不知道当前输入的内容是什么的问题。


TextInputLayout使用

1.导入依赖包
[html]  view plain  copy
  1. dependencies {  
  2.   compile 'com.android.support:appcompat-v7:22.2.0'  
  3.   compile 'com.android.support:design:22.2.0'  
  4. }  

2.布局文件
[html]  view plain  copy
  1. <android.support.design.widget.TextInputLayout  
  2.           android:layout_width="fill_parent"  
  3.           android:id="@+id/til_hint_content"  
  4.           app:errorEnabled="true"  
  5.           android:layout_height="wrap_content">  
  6.   
  7.         <EditText android:id="@+id/et_hint_content"  
  8.             android:layout_width="match_parent"  
  9.             android:layout_height="wrap_content"  
  10.             android:saveEnabled="false"  
  11.             android:maxLength="48"  
  12.             android:digits="1234567890qwertyuiopasdfghjklzxcvbnm "  
  13.             android:hint="你的提示文字"/>  
  14. </android.support.design.widget.TextInputLayout>

TextInputLayout添加逻辑判断


[html]  view plain  copy
  1. et_hint_content.addTextChangedListener(new TextWatcher() {  
  2.       @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
  3.   
  4.       }  
  5.   
  6.       //输入不符合设置的字符,hint会变成红色并提醒  
  7.       @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
  8.         //检查输入是否符合自定义规则  
  9.         if (checkType(charSequence.toString())) {  
  10.           til_hint_content.setErrorEnabled(true);  
  11.           til_hint_content.setError("输入信息有误");  
  12.           return;  
  13.         } else {  
  14.           til_hint_content.setErrorEnabled(false);  
  15.         }  
  16.       }  
  17.   
  18.       @Override public void afterTextChanged(Editable editable) {  
  19.   
  20.       }  
  21.     }); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值