Android学习之Design Support Library中TextInputLayout的使用
来源:互联网 发布:nginx和zookeeper 编辑:程序博客网 时间:2024/06/11 14:40
今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也很简单,故此给大家推荐一下,并记录使用方法。
首先上图来介绍一下它跟我们以往使用的有什么改变。
这里简单看一下,它多了一个提示功能,一般当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="请输入密码" android:inputType="textPassword" android:padding="@dimen/space_16" android:textColor="@color/grey_800" android:textSize="@dimen/text_size_14" /> </android.support.design.widget.TextInputLayout>
实现方式很简单哈,然后该如何在代码里设置呢?
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("请输入用户名"); tlPwd.setHint("请输入密码");
String userName = etUserName.getText().toString().trim(); String password = etPassword.getText().toString(); if (TextUtils.isEmpty(userName)) { tlName.setError("用户名不能为空"); } else { tlName.setErrorEnabled(false); } if (TextUtils.isEmpty(password)) { tlPwd.setError("密码不能为空"); } else { tlPwd.setErrorEnabled(false); }
逻辑嘛,也就是我们平常写的登陆注册逻辑,关键点在于添加了setError(),setErrorEnabled()方法。
好啦,就这么多,有个问题在设置setCounterMaxLengt()后,输入超过设置字符数后,会数组越界,不知道哪位大兄弟可以教我,谢谢!
代码下载
http://pan.baidu.com/s/1pKDg2a3
- Android学习之Design Support Library中TextInputLayout的使用
- Android Design Support Library--TextInputLayout的使用
- Android Support Design Library之TextInputLayout
- Android Design Support Library - TextInputLayout使用
- Android Design Support Library(2)- TextInputLayout的使用
- Android Design Support Library 使用详解二(TextInputLayout)
- Android Design Support Library(一)--TextInputLayout简析
- Android Support Design - TextInputLayout
- Android Support Library-TextInputLayout
- Android Support Design库之TextInputLayout
- Android Design Support Library 中控件的使用简单介绍
- 【Android】Android Design Support Library 的使用
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)
- Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)
- Android Design Support Library 的学习总结
- Android Design Support Library的使用
- Java 实现从数组删除指定位置元素
- Git忽略规则及.gitignore规则不生效的解决办法
- 使用 rplidar测试hector_mapping
- 剑指offer面试题之判断二叉搜索树的后序序列
- Sublime Text3安装 + 破解码(最新) + 插件
- Android学习之Design Support Library中TextInputLayout的使用
- IOS学习之——通讯录的应用
- Log4j的使用
- hdoj 幸福列车 1434 (模拟&队列)
- leetcode---String to Integer (atoi)
- 如何将一个 android工程作为依赖工程
- matlab多变量牛顿方法求解非线性方程组
- Number spiral diagonals
- nyoj拦截导弹