1. 简介

官网开篇

Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text.

Also supports showing an error via setErrorEnabled(boolean) and setError(CharSequence), and a character counter via setCounterEnabled(boolean).

Password visibility toggling is also supported via the setPasswordVisibilityToggleEnabled(boolean) API and related attribute. If enabled, a button is displayed to toggle between the password being displayed as plain-text or disguised, when your EditText is set to display a password.

Note: When using the password toggle functionality, the 'end' compound drawable of the EditText will be overridden while the toggle is enabled. To ensure that any existing drawables are restored correctly, you should set those compound drawables relatively (start/end), opposed to absolutely (left/right).

大致意思:

将布局控件TextInputLayout套在编辑框TextInputEditText或EditText外,当用户编辑时会把指定的hint(无输入时的提示信息)内容上浮显示为标题,支持计数、错误及密码可见控制图标等属性的设置。

详细介绍和用使用案例建议前往官网浏览,毕竟越没有经过加工的信息越接近真相,毕竟写文章是为了自我总结和抛砖引玉,而不是写教程。

用户名和密码常见的输入样式有以下两种(其他还有很多):

    

而利用Material Design的TextInputLayout可以轻松地实现下面这种效果:

只需给TextInputLayout设置好hint属性,那么当其包含的TextInputEditText或EditText处于编辑状态时,hint内容会上浮作为标题显示,如图中的Username。

从图中还可以看到,分别对Username的最大字数限制,Password的是否可见属性进行了设置,其实Email同样设置了格式检测,下面就通过具体的代码来看看用TextInputLayout实现这些功能的便捷与高效。

2. 实例

项目代码放在Github上,感兴趣的朋友自行下载。

先在module build.gradle文件中添加dependencies项:

 compile 'com.android.support:design:25.1.0'

Username的布局代码:

 <android.support.design.widget.TextInputLayout
  android:id="@+id/til_username"
  style="@style/TextInputLayoutStyle"
  app:errorEnabled="true"
  app:counterEnabled="true"
  app:counterMaxLength=""
  app:hintTextAppearance="@style/HintAppearance"
  app:errorTextAppearance="@style/TextErrorAppearance"
  app:counterOverflowTextAppearance="@style/HintErrorAppearance">   <android.support.design.widget.TextInputEditText
    android:id="@+id/et_username"
    android:hint="@string/username"
    style="@style/EditTextStyle" /> </android.support.design.widget.TextInputLayout>

其中用到的五个style定义:

 <style name="TextInputLayoutStyle">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_marginTop">@dimen/edit_top_margin</item>
</style> <style name="EditTextStyle">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:textSize">@dimen/edit_content_size</item>
  <item name="android:textColor">@color/colorEditContent</item>
</style> <style name="HintAppearance" parent="TextAppearance.AppCompat">
  <item name="android:textSize">@dimen/edit_hint_size</item>
  <item name="android:textColor">@color/colorAccent</item>
</style> <style name="HintErrorAppearance" parent="TextAppearance.AppCompat">
  <item name="android:textSize">@dimen/edit_hint_size</item>
  <item name="android:textColor">@color/colorHintError</item>
  <item name="textColorError">@color/colorHintError</item>
</style> <style name="TextErrorAppearance" parent="TextAppearance.AppCompat">
  <item name="android:textSize">@dimen/edit_hint_size</item>
  <item name="android:textColor">@color/colorTextError</item>
  <item name="textColorError">@color/colorTextError</item>
</style>

首先看一下字数限制的文本样式设置:

 app:counterEnabled="true"
app:counterMaxLength=""
app:counterOverflowTextAppearance="@style/HintErrorAppearance"

Username设置了最大字数限制--10,当输入字数超过上限之后就会将hint标题显示为counterOverflowTextAppearance样式。

颜色设置给出截图,可以直观地看到代码对应的颜色:

colorTextError对应编辑框左下角的错误提示,colorHintError对应右下角的字数限制和左上角的hint标题。

从运行结果可以看出,设置了字数限制后,Design会自动在编辑框右下角显示最大字数和当前输入字数(随着输入情况实时变化)。当字数在限制范围内时,hint样式为hintTextAppearance;字数超过之后,hint样式为counterOverflowTextAppearance。当然,如果没有设置counterOverflowTextAppearance属性,Design一般会将错误文本显示为红色,至于不同设备不同主题会有些差别。

接下来看看编辑框左下角的错误提示是怎么设置的,布局代码:

 app:errorEnabled="true"
app:errorTextAppearance="@style/TextErrorAppearance"

还需要在Java代码中指定显示的时机和文本:

 private TextInputLayout mTILUsername;
private EditText mETUsername;
...
mETUsername.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > ) {
mTILUsername.setErrorEnabled(true);
mTILUsername.setError("Username max length is 10.");
} else {
mTILUsername.setErrorEnabled(false);
}
} @Override
public void afterTextChanged(Editable s) {}
});

给编辑框mETUsername设置了编辑状态改变监听器,参数为TextWatcher匿名类,实现onTextChanged方法。当字数超过限制之后将错误属性enable,同时设置错误提示内容;否则,就将错误属性disable。

测试后发现,布局代码中的app:errorEnabled="true"和Java中的mTILUsername.setErrorEnabled(true)是可以不设置的,调用setError方法就够了。

从截图中可以看出,编辑框下方那条线的颜色是跟随errorTextAppearance样式变化的,而不是counterOverflowTextAppearance。

再来看看邮箱格式的检测,这里是利用正则表达式,而且是比较基础的:

 private static final Pattern EMAIL_PATTERN = Pattern.compile(
"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
 if (!EMAIL_PATTERN.matcher(s.toString().trim()).matches()) {
mTILEmail.setErrorEnabled(true);
mTILEmail.setError("Please input correct email.");
} else {
mTILEmail.setErrorEnabled(false);
}

matches方法返回false时表示输入内容不匹配邮箱格式,进而给出错误提示信息。

错误的情况上面已经给出了,正确的情况见下图:

密码编辑框右边带有一个类似眼睛的图标,这就是内容是否可见的开关。布局代码如下:

 <android.support.design.widget.TextInputLayout
android:id="@+id/til_password"
android:layout_below="@id/til_email"
style="@style/TextInputLayoutStyle"
app:errorEnabled="true"
app:passwordToggleEnabled="true"
app:hintTextAppearance="@style/HintAppearance"
app:errorTextAppearance="@style/TextErrorAppearance"> <android.support.design.widget.TextInputEditText
android:id="@+id/et_password"
android:hint="@string/password"
android:inputType="textPassword"
style="@style/EditTextStyle" /> </android.support.design.widget.TextInputLayout>
设置passwordToggleEnabled为true,inputType为textPassword,默认情况下输入内容是以点的形式显示。点击图标之后便会显示明文,再点一下又会显示密文,如此反复切换。明文形式如下:

3. 总结

上面通过例子简单地描述了TextInputLayout的基本用法,官网上的介绍还包含了其他的一些方法,能实现更多的效果。Android Design中文学习资料点这里

Android Material Design--TextInputLayout的更多相关文章

  1. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  2. Android Material Design 兼容库的使用

    Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...

  3. Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决

    Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决 附录1的Android Ripple Effect水 ...

  4. Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计

     Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计 Android Ripple Effect波纹荡漾效果,是Android Materia ...

  5. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...

  6. MaterialEditText——Android Material Design EditText控件

    MaterialEditText是Android Material Design EditText控件.可以定制浮动标签.主要颜色.默认的错误颜色等. 随着 Material Design 的到来, ...

  7. Android Material Design控件使用(二)——FloatButton TextInputEditText TextInputLayout 按钮和输入框

    FloatingActionButton 1. 使用FloatingActionButton的情形 FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App ...

  8. Android Material Design(一)史上最全的材料设计控件大全

    主要内容: 本文将要介绍Material design和Support library控件,主要包括TextInputLayout.SwitchCompat.SnackBar.FloatingActi ...

  9. Android Material Design系列之主题样式介绍说明

    今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...

  10. Android Material design

    1.Material Design:扁而不平 2.Android Support Design 库 之 Snackbar使用及源码分析 3.十大Material Design开源项目,直接拿来用!

随机推荐

  1. 设置node服务器的端口及运行环境

    一般来说,node服务器中通常会看到这样的代码: ``` app.set('port', process.env.PORT || 3000); if ( process.env.NODE_ENV == ...

  2. 【运维监控】四款云服务监控工具介绍:Nagios 、 ganglia、zabbix、onealert

    在我们日常的工作中,有时候需要监控和管理平台的运行状况,而服务运行是否存在异常,是否有软硬件bug等,均需要第一时间知道.对服务状态了如指掌,是一个很重要的事情.那么这个如何做到呢,我们之前在进行私有 ...

  3. 用javascript动态改变网页文字大小

    <script>function setFontSize(size){document.getElementById('bottom').style.fontsize=size+'pt'; ...

  4. QT Creator 快速入门教程 读书笔记(二)

    一 窗口部件 基础窗口部件QWidget类是所有用户界面对象的基类,窗口和控件都是直接或间接继承自 QWidget,下面我们来看一个很简单的例子: 窗口部件(Widget)简称部件,是QT中建立界面的 ...

  5. 一个关于Linux升级Python后yum的小问题

    前几天在自己的阿里云服务器安装好Python3.5.2之后,顺便删除了原有的/usr/bin/python(因为我知道系统自带的是Python2,而且也会有/usr/bin/python2这个文件,所 ...

  6. 老李分享:接口测试之jmeter

    老李分享:接口测试之jmeter   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.移动端自动化测试很多人把他仅仅理解成appu ...

  7. Linux文件管理上

    Linux文件管理   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 对于计算机来说,所谓的数据就是0和1的序列.这样的一个序列 ...

  8. liunx文件与用户和群组

    文件基本属性 在图片中alogrithm的文件属性为drwxrwxr-x,其中d代表此文件为目录. 后面rwx,rwx,r-x分别代表文件所属者(ower),组(group),其他用户(other)的 ...

  9. servlet学习总结(一)——初识Servlet

    Servlet工作过程 当客户端向web服务器发送servlet请求时,web服务器首先检查是否已经加载并创建了servlet实例对象.如果没有会装载并创建该Servlet的一个实例对象.然后调用se ...

  10. HBase应用快速学习

    HBase是一个高性能.面向列.可伸缩的开源分布式NoSQL数据库,是Google Bigtable的开源实现. HBase的思想和应用和传统的RDBMS,NoSQL等有比较大的区别,这篇文章从HBa ...