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. ul ol dl

    1.ul是无序列表,也就是说没有排列限制可以随意加li: <ul> <li>可以随意放置</li> <li>可以随意放置</li> < ...

  2. 【python】二进制、八进制、十六进制表示方法(3.0以上)

    2进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0o开头的: 例如: 0o11则表示十进制的9 (与2.0版本有区别) 16进制是以0x开头的: 例如: 0x11则表示十进制的1 ...

  3. css:中文词不断开,整体换行

    一.问题   关于文字的换行与不换行的问题有些特殊情况,是使用css的word-break等属性实现不了的,下面的情况就证明了: 我们想要的效果是,一个词整体换行或不换行,“兼职测试”可以都换至第二行 ...

  4. 《深入剖析Tomcat》阅读(一)

    第一章 一个简单的Web服务器 该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件.它也可以将传入的HTTP请求字节流显示到控制台上.但是,它并不发送任何头信息到浏览器,如日期或者 ...

  5. Junit4拓展工具JCategory与Testng的Group功能比较

    前言 笔者前段时间分享过一遍文章,关于如何通过引入新注解来扩展Junit4,以解决Process上的问题: JUnit扩展:引入新注解Annotation 最近在跟外面的同事聊的时候,得知Testng ...

  6. Objective-C 入门(给新人的)

    http://www.hengxinsoft.com/2010/12/objective-c-%E5%85%A5%E9%97%A8%EF%BC%88%E7%BB%99%E6%96%B0%E4%BA%B ...

  7. 如何解决 Java 安全问题?

    如何解决 Java 安全问题,目前的应对策略都十分笨拙,往往适得其反.幸运的是,有一种新的方法可以将安全机制嵌入 Java 执行平台--或者更具体地说,嵌入 Java 虚拟机中,进而规避一些「Big ...

  8. 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)

    The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...

  9. mysql 海量数据的存储和访问解决方案

    第1章  引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互 联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的 ...

  10. Storm学习笔记

    1.如何让一个spout并行读取多个流? 方法:任何spout.bolts组件都可以访问TopologyContext.利用这个特性可以让Spouts的实例之间划分流. 示例:获取到storm集群sp ...