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. (11)连个工具类之间的比较4.Collections与Arrays

    集合框架中的工具类:特点:该工具类中的方法都是静态的. Collections:常见方法: 1, 对list进行二分查找: 前提该集合一定要有序. int binarySearch(list,key) ...

  2. java中有符号和无符号数据类型发生转换

    package com.itheima.test01;/* * byte short int long float double 是有符号位的数 * char boolean 是无符号位的数 * 补码 ...

  3. [原创]ASM动态修改JAVA函数之函数字节码初探

    ASM是非常强大的JAVA字节码生成和修改工具,具有性能优异.文档齐全.比较易用等优点.官方网站:http://asm.ow2.org/ 要想熟练的使用ASM,需要对java字节码有一定的了解,本文重 ...

  4. [译]Selenium Python文档:四、元素定位

    要定位一个页面中的元素有多中策略和方法.你可以根据实际情况选择其中最为合适的.Selenium为定位页面元素提供了下面的这些方法: find_element_by_id(使用id) find_elem ...

  5. Android开发遇到手机无法弹出Toast

    今天遇到了一个很奇怪的问题,一个很简单的程序,就是点击按钮弹出一个Toast,但在手机上运行起来,却没有正常弹出Toast 第一反应就是看看是否调用了show方法,很显然,并不是这个低级问题,为了确定 ...

  6. ICC_lab总结——ICC_lab4:时钟树综合

    时钟树综合的理论知识总结在这里:http://www.cnblogs.com/IClearner/p/6580034.html 下面是实践环节:使用ICC进行时钟树综合. 这个实验的目标是: ·设置C ...

  7. windows 下编译php扩展库pecl里的扩展memcache

    Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.简单的说就是将数据调用到内 ...

  8. 27. Remove Element - 移除元素-Easy

    Description: Given an array and a value, remove all instances of that value in place and return the ...

  9. Truncated incorrect DOUBLE value错误

    mysql报错:Truncated incorrect DOUBLE value sql的update语法错误eg: update Person set name = 'auhnayuiL' and ...

  10. [原]C#与非托管——封送和自动封送

    之前说到了如何从C函数声明通过简单的查找替换生成一份C#的静态引用声明(C#与非托管——初体验),因为只是简单说明,所以全部采用的是基础类型匹配和自动封送.自动封送虽然能省去我们不少编码时间,但如果不 ...