今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也非常easy,故此给大家推荐一下。并记录用法。

首先上图来介绍一下它跟我们以往使用的有什么改变。

这里简单看一下,它多了一个提示功能。一般当EditText输入内容后,hint会消失。

TextInputLayout会在输入内容后。提示就会浮动在EditText上方。

当然也支持错误提示。

以下来看实现方式

加入依赖

    //noinspection GradleCompatible
compile 'com.android.support:design:24.0.0-alpha1'

该怎样实现呢?TextInputLayout作为父布局包括EditText即可了

 <android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayouts"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="wrap_content"> <EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space_16"
android:hint="请输入password"
android:inputType="textPassword"
android:padding="@dimen/space_16"
android:textColor="@color/grey_800"
android:textSize="@dimen/text_size_14" />
</android.support.design.widget.TextInputLayout>

实现方式非常easy哈。然后该怎样在代码里设置呢?

TextInputLayout主要包括以下几个方法:


1.setHint()。设置提示信息

2.setError(),设置错误提示信息

3.setErrorEnabled(boolean enabled),设置error是否显示

4.getEditText(),获取自布局EditText

5.setCounterMaxLength(int) 设置可输入最大字符数。并在以下进行统计

6.setCounterEnabled(boolean ); 设置是否统计输入字符数

7.setHintAnimationEnabled(boolean) 设置hint动画是否开启。


效果如上。

 tlName = (TextInputLayout) findViewById(R.id.textInputLayout);
tlPwd = (TextInputLayout) findViewById(R.id.textInputLayouts);
tlName.setHint("请输入username");
tlPwd.setHint("请输入password");

String userName = etUserName.getText().toString().trim();
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(userName)) {
tlName.setError("username不能为空");
} else {
tlName.setErrorEnabled(false);
}
if (TextUtils.isEmpty(password)) {
tlPwd.setError("password不能为空");
} else {
tlPwd.setErrorEnabled(false);
}

逻辑嘛。也就是我们寻常写的登陆注冊逻辑。关键点在于加入了setError()。setErrorEnabled()方法。

好啦,就这么多。有个问题在设置setCounterMaxLengt()后。输入超过设置字符数后,会数组越界,不知道哪位大兄弟能够教我,谢谢!

代码下载

http://pan.baidu.com/s/1pKDg2a3

Android学习之Design Support Library中TextInputLayout的使用的更多相关文章

  1. Android Design Support Library: 学习CoordinatorLayout

    简述 CoordinatorLayout字面意思是"协调器布局",它是Design Support Library中提供的一个超级帧布局,帮助我们实现Material Design ...

  2. Android Design Support Library使用详解——TextInputLayout与TextInputEditText

    TextInputLayout 在谷歌的Material Design中,文本输入是这样表现的:当用户点击输入框想要输入文字时,如果输入框是空的,那么它的提示文字(hint)就会变小并且同时移动到输入 ...

  3. Android Design Support Library(2)- TextInputLayout的使用

    原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50494634 这篇文章介绍下Android Design Support Lib ...

  4. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  5. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

  6. Android Design Support Library——Navigation View

    前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...

  7. Android Design Support Library使用详解——Snackbar

    Google在2015 I/O大会上,给我们带来了更加详细的Material Design规范,同时也引入了Android Design Support Library,为我们提供了基于Materia ...

  8. Android Design Support Library(三)用CoordinatorLayout实现Toolbar隐藏和折叠

    此文的代码在Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果代码的基础上进行修改,如果你没有看过本系列的第一篇文章最好先看一看.Co ...

  9. Android Design Support Library(二)用NavigationView实现抽屉菜单界面

    NavigationView在MD设计中非常重要,之前Google也提出了使用DrawerLayout来实现导航抽屉.这次,在Android Design Support Library中,Googl ...

随机推荐

  1. C#正则表达式匹配HTML中的图片路径

    最近的项目中有个关于网页取图的功能需要我自己开发,那就是用正则表达式来匹配图片标签. 一般来说一个 HTML 文档有很多标签,比如“<html>”.“<body>”.“< ...

  2. Eclipe快捷键

    常用的快捷键其实不多,但是用的少了又忘记了,所以讲常用的快捷键整理一下,方便以后自己来看. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Q 定位到最后 ...

  3. django之创建第7-5-第二种传值方式(time/1232/xiaodneg)

    1.修改views文件 def foo(request,myID,myName): t = loader.get_template("foo.html") user = {&quo ...

  4. Ubuntu x86-64汇编(3) 数值操作指令

    指令标注 Operand Notation 指令instruction即运算operation, 操作的对象为一个或多个运算数operand, 使用不同的标记表示不同的约束 <reg>  ...

  5. (原)使用1080Ti显卡时安装ubuntu16.04.1及驱动的步骤

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6811328.html 参考网址: http://www.cnblogs.com/darkknightz ...

  6. 调试std::string

    首先需要了解std::string的实现原理    string是STL中最为常用的类型,它是模板类basic_string用char类型特化后的结果,下面我们来看一下string类型的基本组成: t ...

  7. linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解

    一.生成任意大小的文件(dd命令): 举例: amosli@amosli-pc:~/learn/example$ ; + records in + records out bytes ( MB/s a ...

  8. /proc/net/sockstat 里的信息是什么意思?

    cat /proc/net/sockstat sockets: used 294 TCP: inuse 35 orphan 0 tw 0 alloc 45 mem 1 UDP: inuse 13 me ...

  9. Linux中的lo回环接口详细介绍

    1.linux的网络接口之扫盲 (1)网络接口的命名 这里并不存在一定的命名规范,但网络接口名字的定义一般都是要有意义的.例如: eth0: ethernet的简写,一般用于以太网接口. wifi0: ...

  10. STDIN_FILENO的作用及与stdin 的区别

    1.STDIN_FILENO的作用 STDIN_FILENO属于系统API接口库,其声明为 int 型,是一个打开文件句柄,对应的函数主要包括 open/read/write/close 等系统级调用 ...