这两个控件也是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. 30年的Hello world

    30 年的 Hello world 转载自:http://www.admin10000.com/document/2398.html 最近我在7月4日这一天所在的那周休假了.休假期间,我利用大把的时间 ...

  2. 中断服务程序(Interrupt Service Routines,ISR)注意事项

    转自ISR之不能做什么 中断是嵌入式系统中重要组成部分,很多编译器开发商都让标准c支持中断,并引入关键字_interrupt.但是: 1.ISR不能有返回值: 2.ISR不能传递参数: 3.ISR应该 ...

  3. PowerDesigner从SqlServer 数据库中导入实体模型

    此篇是之前写的,从我的CSDN博客挖过来的- 一.开启数据库服务并配置ODBC数据源 1.开启数据库服务 (1)通过SQL Server Configuration Manager配置工具启动SQL ...

  4. Spring In Action 第4版笔记-第一章-001架构

    1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...

  5. CCS3.3之DM642开发环境建立

    使用的仿真器是SEED-XDSUSB2.0/5V. 之前用的是CCS2.2,换成了CCS3.3的. 1.安装CCS3.3.38.在我安装完后,并没有急着升级,升级的程序是SR12_CCS_v3.3_S ...

  6. 未能找到类型或命名空间名称DbContext

    Visual Studio调试 .NET 项目时报错: 未能找到类型或命名空间名称“DbContext” 解决办法: 首先 右键 引用——System.Data.Entity 其次,在自己项目里搜索E ...

  7. Android学习笔记12:图像渲染(Shader)

    在Android中,提供了Shader类专门用来渲染图像以及一些几何图形. Shader类包括了5个直接子类,分别为:BitmapShader.ComposeShader.LinearGradient ...

  8. efront二次开发记要

    efront系统是一套开源的在线学习系统,是用PHP编写的,内含“考试”功能.该系统的开源的是社区版,虽然看上去功能强大,但使用起来却很不符合国情.为了让公司使用,先做了一次最简化的二次开发,由于是最 ...

  9. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  10. Reason: Server is in single user mode. Only one administrator can connect at this time

    单击Start→All Programs→Microsoft SQL Server 2008→Configuration Tools→SQL Server Configuration Manager. ...