TextInputLayout是Android 5.0新特性——Material Design中的一个布局控件,主要用来嵌套EditText,实现数据输入时的一些效果,如:

  • 当输入框获取焦点时,输入提示语会动画移动到输入框上方;
  • 当输入框失去焦点时,如果输入框中没有文本,则提示语动画移动回到输入框中;
  • 当输入不合规范时,会在输入框下方显示错误提示语;
  • 当输入的是密码时,可以选择是否显示“显示密码”的按钮以及按钮的图案;
  • 可以显示输入框中当前文本的长度和要求的长度,等。

需要特别注意的是,TextInputLayout作为一个布局控件,不能独立存在,必须嵌套在EditText或TextInputEditText外层。

和其他MD控件一样,使用TextInputLayout之前必须在gradle文件中声明依赖:

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

1、TextInputLayout的属性:

        app:passwordToggleEnabled:是否显示可以查看密码的Toggle按钮
app:passwordToggleDrawable:查看密码的Toggle按钮的图片
注:只有当包裹的EditText或TextInputEditText的InputType是密码格式时才会显示这个图标
app:counterEnabled:是否显示文本长度计数器
app:counterMaxLength:文本长度计数器的最大长度
注:文本长度计数器就是在输入框的右下角显示“X/Y”字样,X表示当前输入框中的文本长度,Y表示规定的输入长度
如果用户输入的长度超过Y,则文本计数器中的文本会变色提示

2、布局代码示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 使用TextInputLayout包裹EditText -->
<android.support.design.widget.TextInputLayout
android:id="@+id/textinputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="6"
app:passwordToggleDrawable="@mipmap/ic_launcher"
app:passwordToggleEnabled="true"> <!-- 这里的TextInputEditText可以使用EditText代替 -->
<android.support.design.widget.TextInputEditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout> </LinearLayout>

运行结果如下图:

3、错误提示:

TextInputLayout支持错误提示,即当经过判断,当前输入的文本不符合要求时,就会在输入框下方显示一行提示,提示输入错误。通过调用TextInputLayout对象的setErrorEnabled()、setError()方法可以实现这一功能。具体代码如下:

        // 是否可以弹出错误提示语
til.setErrorEnabled(true);
// 获取TextInputLayout中包裹的EditText
EditText et = til.getEditText();
// 当EditText中的文本发生变化时处理TextInputLayout的错误提示语及其显隐
et.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) {
try {
if ("".equals(s + "")) {
// 设置错误提示语为null,即不显示错误提示语
til.setError(null);
} else if (s.length() > 6) {
// 如果输入长度超过6位,则显示错误提示
til.setError("密码长度超过上限!");
} else {
Integer.parseInt(s + "");
til.setError(null);
}
} catch (Exception e) {
// 设置错误提示语为具体的文本
til.setError("输入内容不是数字!");
}
} @Override
public void afterTextChanged(Editable s) {
}
});

演示结果如图所示:

以上就是对TextInputLayout的基本用法的介绍,下面贴出码云中的源码,供大家参考。

DEMO地址

【Android - MD】之TextInputLayout的使用的更多相关文章

  1. 【Android - MD】之Snackbar的使用

    Snackbar 是 Android 5.0 新特性--Material Design 中的一个控件,用来代替 Toast ,Snackbar与Toast的主要区别是:Snackbar可以滑动退出,也 ...

  2. 【Android - MD】之CoordinatorLayout的使用

    CoordinatorLayout是Android 5.0新特性--Material Design中的一个布局控件,主要用来协调各个子视图之间的工作,也可以用来作为顶部布局.CoordinatorLa ...

  3. 【Android - MD】之NavigationView的使用

    NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...

  4. 【Android - MD】之TabLayout的使用

    TabLayout是Android 5.0新特性--Material Design中的一个控件,是一个标签页的导航条,常结合ViewPager完成页面导航. 和其他MD控件一样,使用TabLayout ...

  5. 【Android - MD】之FloatingActionButton的使用

    FloatingActionButton(FAB) 是 Android 5.0 新特性--Material Design 中的一个控件,是一种悬浮的按钮. FloatingActionButton 是 ...

  6. 【Android - MD】之RecyclerView的使用

    RecyclerView是Android 5.0新特性--Material Design中的一个控件,它将ListView.GridView整合到一起,可以使用极少的代码在ListView.GridV ...

  7. 【Android - MD】之CardView的使用

    CardView是Android 5.0新特性--Material Design中的一个布局控件,可以通过属性设置显示一个圆角的类似卡片的视图. 1.CardView的属性: app:cardCorn ...

  8. 【Android - 控件】之MD - TextInputLayout的使用

    TextInputLayout是Android 5.0新特性——Material Design中的一个布局控件,主要用来嵌套EditText,实现数据输入时的一些效果,如: 当输入框获取焦点时,输入提 ...

  9. 如何使用android design support library

    Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...

随机推荐

  1. PHP 编译问题PEAR package PHP_Archive not installed的解决

    php 的编译时需要依赖pear package ,目前的问题错误"PEAR package PHP_Archive not installed",已经明显报出这个问题. 因此编译 ...

  2. 安装指南【win10下安装fedora】

    系统安装 安装准备 系统:fedora .Win 10 硬件:U盘一枚.PC一台 软件:UltraISO 安装步骤 使用UltraISO将镜像写入U盘 window10使用磁盘管理,空出一个未分配的区 ...

  3. easy UI demo 含数据库加载示例

    easyUI 部分代码在Googlecode 托管时而被抢此文件包含了所有官方demo,作为备份 下载地址http://pan.baidu.com/s/1pJ9hS5H

  4. 精通 Oracle+Python,第 7 部分:面向服务的 Python 架构

    面向服务的架构 (SOA) 在当今的业务战略中具有至关重要的作用.混搭企业组件已成为所有任务关键的企业应用程序的标准要求,从而确保在企业架构的各层实现顺畅的服务编排.对此,Python 是一个不错的选 ...

  5. 【python】python支持中文变量,醉了

    哈哈 = 1 呜呜 = -1 哈哈 + 呜呜 = 0

  6. python【第十六篇】DOM

    文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口. DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句 ...

  7. Day22 JSONP、瀑布流

    一.JSONP JSONP a.Ajax $.ajax({ url:'/index/', dataType:'json', data:{}, type:'GET', success:function( ...

  8. c++给数组赋值

    c++的基础不牢啊.甚至是c语言也忘记了..所以以后遇到感觉怪异的语法都保存下来,没事翻翻看看 例一 void getSize(int n[]) //把数组名传给函数的形参时候 一维数组[]不用指定大 ...

  9. Js 简单分页(一)

    网上有很多分页的插件 但是没有自己想要的 上代码吧,只有分页部分代码,css 省略了 html <div class="bar2 fr" id="pagecontr ...

  10. 运行在YARN上的MapReduce应用程序(以MapReduce为例)

    client作用:提交一个应用程序查看一个应用程序的运行状态(通过application master) 第一步:提交MR程序到ResourceManager,ResourceManager为这个应用 ...