前言

前两次,我们学习了

今天我们继续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实现酷市场登录效果的更多相关文章

  1. Android Material Design控件学习(一)——TabLayout的用法

    前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...

  2. Android Material Design控件学习(二)——NavigationView的学习和使用

    前言 上次我们学习了TabLayout的用法,今天我们继续学习MaterialDesign(简称MD)控件--NavigationView. 正如其名,NavigationView,导航View.一般 ...

  3. Android Material Design控件使用(一)——ConstraintLayout 约束布局

    参考文章: 约束布局ConstraintLayout看这一篇就够了 ConstraintLayout - 属性篇 介绍 Android ConstraintLayout是谷歌推出替代PrecentLa ...

  4. Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout

    使用下拉刷新SwipeRefreshLayout 说明 SwipeRefreshLayout是Android官方的一个下拉刷新控件,一般我们使用此布局和一个RecyclerView嵌套使用 使用 xm ...

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

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

  6. Android Material Design控件使用(三)——CardView 卡片布局和SnackBar使用

    cardview 预览图 常用属性 属性名 说明 cardBackgroundColor 设置背景颜色 cardCornerRadius 设置圆角大小 cardElevation 设置z轴的阴影 ca ...

  7. Android Material Design 控件常用的属性

    android:fitsSystemWindows="true" 是一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为t ...

  8. 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用

    Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...

  9. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

随机推荐

  1. in linux system of ftp command

    一口流利的english title 常用命令: ftp baidu.com ftp>ls ftp>cd directory ftp>get filename 下载 ftp>p ...

  2. redis过期回调以及键空间通知

    背景 最近需要涉及一个定时通知的业务,之前的办法是采用定时任务,每秒查询一次.后来了解到Redis的键空间通知机制,其中的过期通知,和业务非常贴合. 键空间通知 下面是Redis中文文档的介绍 键空间 ...

  3. Android——音乐播放器完善——进度条显示当前播放进度,加可拖动进度条(未待解决完问题)

    效果: 问题:可拖动进度条随进度条移动时,会致使音乐卡顿(待解决) xml <?xml version="1.0" encoding="utf-8"?&g ...

  4. CAS (10) —— JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法

    CAS (10) -- JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法 jboss版本: jb ...

  5. Centos 安装jdk jre

    下载rpm http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html wget ht ...

  6. R-Sys.time计算程序运行时间

    R用Sys.time()可以查看当前系统时间程序开始时记录: timestart<-Sys.time()程序临结束时记录: timeend<-Sys.time()程序运行时间: runni ...

  7. Qt 程序打包发布总结

    1.  概述 当我们用QT写好了一个软件,要把你的程序分享出去的时候,不可能把编译的目录拷贝给别人去运行.编译好的程序应该是一个主程序,加一些资源文件,再加一些动态链接库,高大上一些的还可以做一个安装 ...

  8. imx6 lvds0 lvds1 display

    最近调试imx6的屏幕显示,笔记记录于此. 官方文档关于uboot参数的介绍: sin和dul参数已经测试过,sep和spl还没有验证成功. 1 单屏显示 说明:输入命令并按确定键, 观察系统启动过程 ...

  9. nodemon是个好东西

    不说话,直接上图: 安装 使用

  10. Google File System 学习

    摘要 Google的人设计并实现了Google File System,一个可升级的分布式文件系统,用于大的分布式数据应用.可以运行在廉价的日用硬件上,具备容错性,且为大量客户端提供了高聚合的性能. ...