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

  • TextInputLayout
TextInputLayout功能很easy,就是用于用户在EditText中输入时hint的提示和错误的提示。
先来看看效果图吧:

从上图非常明显的看出:
1、当EditText获得焦点时候。TextInputLayout会在左上角默认的生成一个Label用来显示EditText中hint的内容,所以当用户输入时候hint内容会浮动到左上角,这极大便利了用户输入提交数据的体验。

2、当EditText中输入的内容不合法时,TextInputLayout便会在EditText的左下角用红色显示错误的提示信息。这是怎么回事呢?我们来看看TextInputLayout的源代码。发现TextInputLayout中有个setErrorEnabled(boolean enabled)方法,意思就是是否设置错误提示信息:

从源代码中我们非常清晰的看到TextInputLayout的错误提示的工作原理了。总结就是:假设传入true,则TextInputLayout会在左下角生成一个TextView用来显示错误信息。假设传入false,则移除TextView从而不显示错误信息。从源代码中我们还能够看到setError(CharSequence error);这种方法,顾名思义,这种方法就是当我们设置须要显示错误提示信息setErrorEnabled(true),我们再使用setError()方法把我们须要设置的错误提示信息传入到TextView中显示出来。
以下来看看怎么用代码实现:
布局activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <android.support.design.widget.TextInputLayout
android:id="@+id/textInput_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="user_name" />
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:id="@+id/textInput_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="user_password" />
</android.support.design.widget.TextInputLayout> </LinearLayout>

在代码中实现:

 TextInputLayout mTextInputLayoutName = (TextInputLayout) findViewById(R.id.textInput_layout_name);
TextInputLayout mTextInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInput_layout_password);
//mTextInputLayoutName.getEditText()返回的是它的子EditText控件,一个TextInputLayout仅仅能包括一个EditText控件
mTextInputLayoutName.getEditText().addTextChangedListener(new MyTextWatcher(mTextInputLayoutName, "username长度不能小于6位"));
mTextInputLayoutPassword.getEditText().addTextChangedListener(new MyTextWatcher(mTextInputLayoutPassword, "密码长度不能小于6位"));

MyTextWatcher代码:

class MyTextWatcher implements TextWatcher {
private TextInputLayout mTextInputLayout;
private String errorInfo; public MyTextWatcher(TextInputLayout textInputLayout, String errorInfo) {
this.mTextInputLayout = textInputLayout;
this.errorInfo = errorInfo;
} @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) {
if (mTextInputLayout.getEditText().getText().toString().length() < 6) {
mTextInputLayout.setErrorEnabled(true);//是否设置错误提示信息
mTextInputLayout.setError(errorInfo);//设置错误提示信息
} else {
mTextInputLayout.setErrorEnabled(false);//不设置错误提示信息
}
}
}

当中,须要注意两点:

1、TextInputLayout布局中仅仅能包括一个EditText子View,不能包括多个EditText。

2、TextInputLayout中有个方法getEditText();该方法返回的是它的子EditText对象,所以我们可通过mTextInputLayout.getEditText();来得到EditText对象。不须要findViewById找了。
3、设置错误提示信息时一定要先setErrorEnabled(true);再设置setError(...);由于TextView仅仅在setErrorEnabled()方法中创建。必须创建好TextView才干往TextView上设置信息。而不须要设置时直接setErrorEnabled(false);就可以,由于它自身会remove移除TextView。

  • Snackbar

Snackbar是带有动画效果的提示条,它极具美观效果,它出如今屏幕的底部。它和Toast类似,只是却有的地方和Toast不同,Snackbar能够加入button。能够支持滑动删除,也能够不作不论什么操作自身会在几秒(1.5s或2.75s)后消失。

最值得注意的一点就是Snackbar不能同一时候显示多个,仅仅能一次显示一个。而Toast能够同一时候显示多个这和Toast不同。


如今来看看怎么实现吧:
1、普通Snackbar:




代码实现:
Snackbar.make(rootView, "This is a SnackBar", Snackbar.LENGTH_SHORT).show();//第一个參数是SnackBar显示在哪个视图上,必须设置,不能为null,一个界面也不能同一时候显示两个SnackBar

当中特别须要注意第一个參数。它表示Snackbar是显示在哪个视图上,不可为null,比方我们能够通过页面中其他的view.getRootView()得到根视图,再把rooView设置进去就可以。


2、带有button的Snackbar:

代码实现:
//带button的SnackBar
Snackbar.make(rootView,"带button的SnackBar",Snackbar.LENGTH_SHORT).setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something ...
}
}).show();

好了,这两个控件就讲完了。。。

当中别忘记加入对design Library的依赖哦!

因为比較简单。就不上传源代码了。



Material Design之TextInputLayout、Snackbar的使用的更多相关文章

  1. Material Design之TextInputLayout使用示例

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

  2. Android Material Design 系列之 SnackBar详解

    SnackBar是google Material Design提供的一种轻量级反馈组件.支持从布局的底部显示一个简洁的提示信息,支持手动滑动取消操作,同时在同一个时间内只能显示一个SnackBar. ...

  3. Material Design学习-----TextInputLayout

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

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

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

  5. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...

  6. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  7. Material Design: NavigationView FlaotingActionBar SnackBar采用

    转载 请明确说明 MingsangAndroid 本文介绍了Design Support Library的引入 拥抱Android Design Support Library新变化(导航视图.悬浮A ...

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

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

  9. Material Design (二),TextInputLayout的使用

    前言  一般登录注冊界面都须要EditText这个控件来让用户输入信息,同一时候我们通常会设置一个标签(使用TextView)和EditText的hint属性来提示用户输入的内容,而设计库中高级组件T ...

随机推荐

  1. LED汽车前大灯

    一.LED汽车前大灯遇到问题.分析和解决 问题1: 当电源电压增大时,LED等闪烁,而且电源电压增大的越多闪烁的频率越低. 原因分析: 电源电压从12V升高到24V过程中,开关MOS管的Vds增大,Q ...

  2. Java设计模式之——单例模式

    引自百度百科: 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类. 通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系 ...

  3. 在Eclipse中搭建C/C++环境

    在Eclipse中搭建C/C++环境 本文地址:http://blog.csdn.net/you_and_me12/article/details/7389934 习惯了使用eclipse编程后,现在 ...

  4. [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】

    题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...

  5. MATLAB conv2卷积的实现

    MATLAB conv2卷积的实现 二维卷积的算法原理比较简单,参考任意一本数字信号处理的书籍,而matlab的conv2函数的滤波有个形状参数,用下面的一张图很能说明问题: 这里给出一种最原始的实现 ...

  6. Web-Scale IT:对企业的影响

    本文翻译自文章Web-Scale IT: The Enterprise Impact. 作者Brendan Ziolo 在通信.网络和安全行业有着近20年的经验,在 Sipera Systems,Ce ...

  7. 使用@ResponseBody 出现错误Could not find acceptable representation

    org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio ...

  8. org.springframework.web.bind.ServletRequestDataBinde

    org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...

  9. 【HDOJ】3061 Battle

    Dinic解网络流模板题目.队列用STL就TLE... /* 3061 */ #include <iostream> #include <string> #include &l ...

  10. ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页!

    转自:http://www.cnblogs.com/lt-style/p/3457399.html 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号.班级名称 开发过程: 1. ...