Android Design Support Library——TextInputLayout
前沿
上一篇介绍了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的更多相关文章
- Android Design Support Library(2)- TextInputLayout的使用
原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50494634 这篇文章介绍下Android Design Support Lib ...
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- 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 ...
- Material Design 开发利器:Android Design Support Library 介绍
转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...
- Android Design Support Library——Navigation View
前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...
- 如何使用android design support library
Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...
- Android Design Support Library 中控件的使用简单介绍(一)
Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...
随机推荐
- Android 学习笔记之Volley开源框架解析(五)
学习内容: 1.PoolingByteArrayOutputStream 2.ByteArrayPool 3.HttpStack 4.HurlStack 5.HttpHeaderParser 前面 ...
- 终极事务处理(XTP,Hekaton)——万能大招?
在SQL Server 2014里,微软引入了终极事务处理(Extreme Transaction Processing),即大家熟知的Hekaton.我在网上围观了一些文档,写这篇文章,希望可以让大 ...
- 就这样获取文件的MD5和大小
纠结真蛋疼 判断一件事值不值得去做的唯一标准是这件事是不是令我纠结.如果纠结了,就不去做了!但是,人总要活着,又能怎样.谁说男人就没有那么几天...... 从极速妙传说起 在现在各大厂商都推出免费云盘 ...
- [Bootstrap]7天深入Bootstrap(4)CSS组件
Bootstrap框架的三大核心之二:组件. 组件也是最核心的地方,因为绝大部分的网页都必须利用组件才能构建出绚丽的页面. 组件包括:Icon图标(Glyphicon). 下拉菜单(Dropdown) ...
- sprint3与总结
backlog-看板-燃尽图-每日立会 github:https://github.com/alfredzhu/team-work 总结:这种团队合作的方式很好,大家在一起沟通,相互交流想法,一起解决 ...
- 0527Sprint总结,读书笔记与提问
第八章主要软件团队如何准确全面的找到人们对于软件五花八门的需求,主要有以下几个步骤: 1.获取和引导需求 2.分析和定义需求 3.验证需求 4.在软件产品的生命周期中管理需求 . 接下来讲了软件产品的 ...
- 使用VS2010开发Qt程序的一点经验
导读 相比于Qt Creator,我更喜欢用VS2010来进行开发.虽然启动时间相对较慢,但是VS下强大的快捷键和丰富的插件,以及使用多年的经验,都让我觉得在开发过程中得心应手.其中最重要的一点是,有 ...
- DBSet Class(EF基础系列11)
Method Name Return Type Description Add Added entity type Adds the given entity to the cont ...
- c# 编程语言 编译器 Roslyn
4 月3日,微软向公众发布了Roslyn编译器项目,该项目采用了Apache开源许可协议.C#的创始人 Anders Hejlsberg在Build大会的第二场主题演讲中将这一令人震惊的消息公之于众. ...
- 重构第28 天 重命名bool方法(Rename boolean method)
详解:本文中的”为布尔方法命名”是指如果一个方法带有大量的bool 参数时,可以根据bool 参数的数量,提取出若干个独立的方法来简化参数. 理解: 我们现在要说的重构并不是普通字面意义上的重构,它有 ...