Material Design之TextInputLayout、Snackbar的使用
这两个控件也是Google在2015 I/O大会上公布的Design Library包下的控件,使用比較简单,就放在一起讲了,但有的地方也是须要特别注意一下。
- TextInputLayout
<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);//不设置错误提示信息
}
}
}
当中,须要注意两点:
- Snackbar
Snackbar是带有动画效果的提示条,它极具美观效果,它出如今屏幕的底部。它和Toast类似,只是却有的地方和Toast不同,Snackbar能够加入button。能够支持滑动删除,也能够不作不论什么操作自身会在几秒(1.5s或2.75s)后消失。
最值得注意的一点就是Snackbar不能同一时候显示多个,仅仅能一次显示一个。而Toast能够同一时候显示多个这和Toast不同。
代码实现:
Snackbar.make(rootView, "This is a SnackBar", Snackbar.LENGTH_SHORT).show();//第一个參数是SnackBar显示在哪个视图上,必须设置,不能为null,一个界面也不能同一时候显示两个SnackBar
当中特别须要注意第一个參数。它表示Snackbar是显示在哪个视图上,不可为null,比方我们能够通过页面中其他的view.getRootView()得到根视图,再把rooView设置进去就可以。
//带button的SnackBar
Snackbar.make(rootView,"带button的SnackBar",Snackbar.LENGTH_SHORT).setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something ...
}
}).show();
好了,这两个控件就讲完了。。。
Material Design之TextInputLayout、Snackbar的使用的更多相关文章
- Material Design之TextInputLayout使用示例
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...
- Android Material Design 系列之 SnackBar详解
SnackBar是google Material Design提供的一种轻量级反馈组件.支持从布局的底部显示一个简洁的提示信息,支持手动滑动取消操作,同时在同一个时间内只能显示一个SnackBar. ...
- Material Design学习-----TextInputLayout
TextInputLayout是为EditText提供了一种新的实现和交互方式.在传统的EditText中存在一个hint属性,是说在editext中没有内容时,默认的提示信息.当想edittext中 ...
- 用户登录(Material Design + Data-Binding + MVP架构模式)实现
转载请注明出处: http://www.cnblogs.com/cnwutianhao/p/6772759.html MVP架构模式 大家都不陌生,Google 也给出过相应的参考 Sample, 但 ...
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...
- Material Design: NavigationView FlaotingActionBar SnackBar采用
转载 请明确说明 MingsangAndroid 本文介绍了Design Support Library的引入 拥抱Android Design Support Library新变化(导航视图.悬浮A ...
- Android Material Design控件使用(二)——FloatButton TextInputEditText TextInputLayout 按钮和输入框
FloatingActionButton 1. 使用FloatingActionButton的情形 FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App ...
- Material Design (二),TextInputLayout的使用
前言 一般登录注冊界面都须要EditText这个控件来让用户输入信息,同一时候我们通常会设置一个标签(使用TextView)和EditText的hint属性来提示用户输入的内容,而设计库中高级组件T ...
随机推荐
- LED汽车前大灯
一.LED汽车前大灯遇到问题.分析和解决 问题1: 当电源电压增大时,LED等闪烁,而且电源电压增大的越多闪烁的频率越低. 原因分析: 电源电压从12V升高到24V过程中,开关MOS管的Vds增大,Q ...
- Java设计模式之——单例模式
引自百度百科: 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类. 通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系 ...
- 在Eclipse中搭建C/C++环境
在Eclipse中搭建C/C++环境 本文地址:http://blog.csdn.net/you_and_me12/article/details/7389934 习惯了使用eclipse编程后,现在 ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- MATLAB conv2卷积的实现
MATLAB conv2卷积的实现 二维卷积的算法原理比较简单,参考任意一本数字信号处理的书籍,而matlab的conv2函数的滤波有个形状参数,用下面的一张图很能说明问题: 这里给出一种最原始的实现 ...
- Web-Scale IT:对企业的影响
本文翻译自文章Web-Scale IT: The Enterprise Impact. 作者Brendan Ziolo 在通信.网络和安全行业有着近20年的经验,在 Sipera Systems,Ce ...
- 使用@ResponseBody 出现错误Could not find acceptable representation
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio ...
- org.springframework.web.bind.ServletRequestDataBinde
org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...
- 【HDOJ】3061 Battle
Dinic解网络流模板题目.队列用STL就TLE... /* 3061 */ #include <iostream> #include <string> #include &l ...
- ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页!
转自:http://www.cnblogs.com/lt-style/p/3457399.html 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号.班级名称 开发过程: 1. ...