Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言
前两次,我们学习了
今天我们继续MD控件的学习和使用。在学习之前,我们先来看一下酷市场的登录效果。

实现这种效果的正是我们今天的主角——TextInputLayout。
学习
不管学习什么,首先看它的官方文档。这是最权威,最高效的学习途径。
文档地址:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

可以看到,TextInputLayout继承自LinearLayout。一般将EditText和它的子类包含在内,以便用户在输入文本时,且当提示文本被隐藏时显示一个浮动的标签。
也支持通过setErrorEnabled(boolean)和setError(CharSequence)方法来显示错误提示。
用法
TextInputLayout使用起来非常简单,两部搞定
- 1.编写XML布局文件,将EditText包含在内即可。
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_name"
android:textColorHint="@color/colorGray"
/>
</android.support.design.widget.TextInputLayout>
- 2.编写逻辑代码
本来不需要这一步的,因为这控件存在一个bug,当清空之前输错的内容后,提示错误的红色文字并不会消失。
我在Google和StackOverFlow上找了很久,这貌似是Google的bug。可参考 https://code.google.com/p/android/issues/detail?id=190355
为了解决这个bug,就需要我们监听EditText的输入变化了,具体处理看代码。
mEditTextName.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) {
}
@Override
public void afterTextChanged(Editable s) {
checkName(s.toString(),false);
}
});
/**
*若用户名为空且用户是想登录,提示错误,返回false。不为空,错误设为null
*/
private boolean checkName(CharSequence name,boolean isLogin) {
if(TextUtils.isEmpty(name)) {
if(isLogin) {
mTextInputLayoutName.setError(getString(R.string.error_login_empty));
return false;
}
}else{
mTextInputLayoutName.setError(null);
}
return true;
}
实现效果:

具体代码我上传到Github了,https://github.com/JohnTsaiAndroid/CoolMarket
欢迎star&fork
Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果的更多相关文章
- Android Material Design控件学习(一)——TabLayout的用法
前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...
- Android Material Design控件学习(二)——NavigationView的学习和使用
前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...
- Android Material Design控件使用(一)——ConstraintLayout 约束布局
参考文章: 约束布局ConstraintLayout看这一篇就够了 ConstraintLayout - 属性篇 介绍 Android ConstraintLayout是谷歌推出替代PrecentLa ...
- Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout
使用下拉刷新SwipeRefreshLayout 说明 SwipeRefreshLayout是Android官方的一个下拉刷新控件,一般我们使用此布局和一个RecyclerView嵌套使用 使用 xm ...
- Android Material Design控件使用(二)——FloatButton TextInputEditText TextInputLayout 按钮和输入框
FloatingActionButton 1. 使用FloatingActionButton的情形 FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App ...
- Android Material Design控件使用(三)——CardView 卡片布局和SnackBar使用
cardview 预览图 常用属性 属性名 说明 cardBackgroundColor 设置背景颜色 cardCornerRadius 设置圆角大小 cardElevation 设置z轴的阴影 ca ...
- Android Material Design 控件常用的属性
android:fitsSystemWindows="true" 是一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为t ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
随机推荐
- 【转】亲测plsql Developer配置免安装oralce客户端步骤
原文地址:http://blog.csdn.net/bushy0401/article/details/11869461 再次用到Oracle了,机器上面也没有oracle客户端,还得去网上下载,直接 ...
- PowerDesigner导出表到HTML或word(实测有效)
推荐生成HTML,因为看起来更加简洁 一.模版修改 在导出表时,powerdesigner默认为我们提供了很多的模版,在工具栏中选择[Report--->Report Template]即可看到 ...
- facebook工具xhprof的安装与使用-分析php执行性能
下载源码包的网址 http://pecl.php.net/package/xhprof
- git 管理 Linux 文件系统
git 管理 Linux 文件系统有两个问题,第一个是 Linux 根文件系统的 root 权限问题,第二个就是git不上传空文件问题. 一. root权限问题的话就得在 root 用户下git , ...
- shell文本处理
1.文件整理employee文件中记录了工号和姓名employee.txt:100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma ...
- linq to sql之组装where条件下的'或'语句
之前遇到过类似的需求,即前台传入几个过滤条件,后台动态组装where. 例如,前台传入name='张三',age=10, 其余的字段,类似email,QQ之类的本次查询时不做过滤. 用linq to ...
- 1px的border
css中是这样写的: div{ border-bottom: 1px solid #dfe5e4; } 但在手机上,像素比不为 1 ,由于 webview 的灰度渲染, border 一般会显示为 2 ...
- 编译和运行java文件 找不到或无法加载主类
这边提供一个关于程序中含有package关键字,使用“终端”运行程序时出现“找不到或无法加载主类”,而使用Eclipse软件可以正常运行程序的可能解决办法. 例如程序名为HelloWorldTest. ...
- Java设计模式(13)模板模式(Template模式)
Template模式定义:定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中. 其实Java的抽象类本来就是Template模式,因此使用很普遍.而且很容易理解和使用,我们直接以示例开始: pu ...
- Java设计模式(11)外观模式(Facade模式)
外观模式(Facade)的定义:为子系统中的一组接口提供一个一致的界面. Facade一个典型应用就是数据库JDBC的应用,如下例对数据库的操作: public class DBCompare { C ...