前言

 一般登录注冊界面都须要EditText这个控件来让用户输入信息,同一时候我们通常会设置一个标签(使用TextView)和EditText的hint属性来提示用户输入的内容,而设计库中高级组件TextInputLayout则专门为EditText设计的。即通过使用TextInputLayout包裹EditText实现当用户開始输入时hint属性值将显示在EditText上面作为一个提示标签,这个过程还带有动画效果,这样就避免了用户输入时输入提示消失的情况,同一时候。还能够更好地向用户提示错误输入信息。

TextInputLayout的两个功能:

  1. 给EditText加入一个带有动画效果的提示标签(利用EditText的hint属性的值作为提示标签内容);
  2. 处理错误输入。将错误输入提示信息显示在EditText附近。便于提示用户更好地完毕输入。

1. 实现浮动标签提示效果

 TextInputLayout和FrameLayout一样都是一个ViewGroup,而TextInputLayout包裹的是EditText,而且会将EditText的android:hint属性值作为提示标签,所以。使用很easy,例如以下:

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_margin="20dp"
android:id="@+id/usernameWraper"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="请输入用户名"
android:layout_height="wrap_content"/> </android.support.design.widget.TextInputLayout>

用TextInputLayout包裹EditText并给EditText设置了hint属性后。这个EditText就带有了浮动提示标签的效果了,为了更好地看到效果。丰富一下这个xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:background="#ff9900"
android:layout_height="200dp"> <TextView
android:text="用户登录"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:layout_height="wrap_content"/>
</RelativeLayout> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_margin="20dp"
android:id="@+id/usernameWraper"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="请输入用户名"
android:layout_height="wrap_content"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_margin="20dp"
android:id="@+id/passwordWraper"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="请输入密码"
android:layout_height="wrap_content"/> </android.support.design.widget.TextInputLayout> <Button
android:layout_width="match_parent"
android:layout_margin="20dp"
android:text="登录"
android:id="@+id/btn_login"
android:layout_height="wrap_content"/>
</LinearLayout>

2. 显示错误输入信息

 TextInputLayout使得我们在验证输入数据是能够更加方便地显示错误输入提示,这样能够使得输入更加友好。

对于处理错误信息,TextInputLayout提供了两个方法:

  1. setError(String message):设置错误提示信息。这个信息将会显示在EditText附近。

  2. setErrorEnabled(boolean enabled):设置错误信息不能够用,也就是移除setError(String message)加入的错误提示信息。

代码样例:

package com.example.lt.meterialdesign;

import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button; public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private TextInputLayout mUsernameWraper;
private TextInputLayout mPasswordWraper; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameWraper = (TextInputLayout) findViewById(R.id.usernameWraper);
mPasswordWraper = (TextInputLayout) findViewById(R.id.passwordWraper); Button btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
} @Override
public void onClick(View v) {
// TextInputLayout能够取得包裹的EditText
String username = mUsernameWraper.getEditText().getText().toString().trim();
String password = mPasswordWraper.getEditText().getText().toString().trim();
if(TextUtils.isEmpty(username)){
mUsernameWraper.setError("用户名不能为空");
return;
}else{
// 移除错误提示信息
mUsernameWraper.setErrorEnabled(false);
}
if(TextUtils.isEmpty(password)){
mPasswordWraper.setError("密码不能为空");
return;
}else{
// 移除错误提示信息
mPasswordWraper.setErrorEnabled(false);
}
}
}

这里仅仅是对username与password是否为空进行了推断。假设要指定格式能够结合正則表達式验证数据格式。对于EditText能够给它加入文本改变监听addTextChangedListener,当用户改变输入的文本后进行数据格式的验证。然后更加情况显示输入提示。

执行效果:

能够看到,当我们開始在EditText中输入信息的时候,EditText的hint属性值将会显示在EditText上面,而且带有一个动画效果,显示为一个浮动标签。而且,当输入的数据格式不正确时,还会显示错误提示在EditText以下。

Material Design (二),TextInputLayout的使用的更多相关文章

  1. Android(Lollipop/5.0) Material Design(二) 入门指南

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简介 Android(Lollipop/5.0)Material Design(二) ...

  2. Material Design之TextInputLayout使用示例

    Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...

  3. Material Design之TextInputLayout、Snackbar的使用

    这两个控件也是Google在2015 I/O大会上公布的Design Library包下的控件,使用比較简单,就放在一起讲了,但有的地方也是须要特别注意一下. TextInputLayout Text ...

  4. Material Design学习-----TextInputLayout

    TextInputLayout是为EditText提供了一种新的实现和交互方式.在传统的EditText中存在一个hint属性,是说在editext中没有内容时,默认的提示信息.当想edittext中 ...

  5. Android(Lollipop/5.0) Material Design(六) 使用图像

    Material Design列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design(二 ...

  6. 用户登录(Material Design + Data-Binding + MVP架构模式)实现

    转载请注明出处: http://www.cnblogs.com/cnwutianhao/p/6772759.html MVP架构模式 大家都不陌生,Google 也给出过相应的参考 Sample, 但 ...

  7. Android(Lollipop/5.0) Material Design(一) 简单介绍

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  8. Android(Lollipop/5.0) Material Design(四) 创建列表和卡片

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

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

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

随机推荐

  1. ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器

    我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...

  2. yum升级kernel

    # uname -a Linux host -.el6.x86_64 # SMP Fri May :: BST x86_64 x86_64 x86_64 GNU/Linux # cat /etc/re ...

  3. 让chrome支持本地Ajax请求

    Chrome的安全机制不能支持ajax的本地访问, 例如: 在JavaScript里面访问 URL:file:///E:/test.html,Chrome 浏览器报错:XMLHttpRequest c ...

  4. v4l2 spec 中文 Ch01【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/6638245 目录(?)[-] Chapter 1 通用APICommon API El ...

  5. windows下phpstorm的常用快捷键及使用技巧

    1.pubf+回车  声明一个方法 2./ ** 回车   生成注释 3.Ctrl +g  跳转行 4.Ctrl + Alt + L 代码格式化 5.Ctrl +D 复制当前行到下一行 6.Ctrl  ...

  6. 这绝对是有史以来最详细的web前端学习路线

    定要善用开发者工具.firefox的firebug和Chrome的F12都是很好的选择,用好了这个必会发现他带给你的帮助比看一本书更多.你把firebug摸透了你还担心对DOM理解不够?考虑到未来,h ...

  7. Ajax不能跨域访问的解决方案

      文章介绍 这是一篇,引导文吧... 因为写这篇文章时,实在想不出该如何分序.因此以实现跨域访问为目的,从基础知识往上写.最后以百度搜索智能提示为例,来讲解跨域的具体应用! 内容 首先,我们得明确什 ...

  8. (1)WPF概述

    一.wpf特点 winfrom使用GID/GID+ 图形引擎,wpf使用directx的图形引擎 通过directx提供硬件加速 类型web的布局模型 丰富的绘图模型.文本模型, 支持音频视频 可创建 ...

  9. spoj - Grass Planting(树链剖分模板题)

    Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些 ...

  10. Apache优化建议

    Apache是Web服务器软件,它最常见是搭配PHP开发语言去使用.今天,小编根据Apache官方手册再结合实际,整理出下面这些优化建议,希望对大家的Apache服务器的运行效率有效果. 1.控制Ma ...