前沿

  上一篇介绍了NavigationView的主要使用方式,本章主要介绍TextInputLayout的使用方式。

  TextInputLayout——EditText悬浮标签

  TextInputLayout主要作为EditText的父容器来使用,不能单独使用。TextInputLayout解决了EditText输入后hint文字消失的情况,当用户在EditText开始输入后,hint部分的文字会浮动到EditText的上方,而且还可以添加输入错误时的提示信息,显示在editText的下方,效果如下:

  

  使用步骤:

  (1)xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>

  (2)Java代码调用

final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.usernameWrapper);
textInputLayout.setHint("Username");
EditText editText = textInputLayout.getEditText();//直接通过getEditText()获得EditText控件即可 editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length() > 3) {//这里可以加入正则判断
textInputLayout.setError("Password error");
textInputLayout.setErrorEnabled(true); //一定要在setError方法之后执行才可
} else {
textInputLayout.setErrorEnabled(false);//不满足条件需要设置为false
}
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}

  此外可以通过TextInputLayout如下两个属性来设置hint和error的字体

app:errorTextAppearance="@style/FloatingStyle"
app:hintTextAppearance="@style/FloatingStyle"

  /style/FloatingStyle

 <style name="FloatingStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">12sp</item>
</style>

Android Design Support Library——TextInputLayout的更多相关文章

  1. Android Design Support Library(2)- TextInputLayout的使用

    原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50494634 这篇文章介绍下Android Design Support Lib ...

  2. Android Design Support Library使用详解

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

  3. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  4. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

  5. Codelab for Android Design Support Library used in I/O Rewind Bangkok session

    At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...

  6. Material Design 开发利器:Android Design Support Library 介绍

    转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...

  7. Android Design Support Library——Navigation View

    前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...

  8. 如何使用android design support library

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

  9. Android Design Support Library 中控件的使用简单介绍(一)

    Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...

随机推荐

  1. jquery选中下拉列表的某个值

    $('#villageToiletAnn').val('xxx'); id是select的ID,不是option的ID

  2. .Net魔法堂:史上最全的ActiveX开发教程——开发篇

    一.前言 在设计某移动内部自动化运维平台时,经综合考虑终端机性能和功能需求等因素后,决定采用B/S模式,并且浏览器通过ActiveX组件实现与服务器Agent作P2P的通讯.好处,整个平台以网页形式存 ...

  3. EncryptTransform

    internal class EncryptTransform { //private const int c_MaxLengthOf_IV_DES = 4; //private const int ...

  4. ok6410 android driver(4)

    Install busybox for goldfish/phone 1. Download busybox source code http://www.busybox.net/ 2. Decomp ...

  5. SQL年月日方面的查询信息

    这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DAT ...

  6. Entity FrameWork 延迟加载本质(二)

    1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...

  7. P6 EPPM R16.1安装与配置指南(二)

    P6 EPPM R16.1安装与配置指南(一) http://www.cnblogs.com/endv/p/5634620.html P6 EPPM R16.1安装与配置指南(二) 环境变量配置 新建 ...

  8. Silverlight开源项目与第三方控件收集

    http://easysl.codeplex.com/ http://compositewpf.codeplex.com/ http://silverlight.codeplex.com/releas ...

  9. Asp.NET MVC JSON序列化问题

    最近在做项目的时候遇到一个JSON序列化问题. 环境:ASP.NET MVC 4.0 数据库:SQL 2008 在将获取的数据从后台以json的形式传给前台的easyui treegrid绑定的时候通 ...

  10. JS Date.Format

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...