TextInputLayout控件
来源:互联网 发布:淘宝卖家的需求分析 编辑:程序博客网 时间:2024/05/16 18:31
转载自http://blog.csdn.net/feiduclear_up/article/details/46500865
该控件是用于EditView输入框的,主要解决之前EditView在获得焦点编辑时hint属性提示语消失,这一点在一个页
面有多个EditView输入框的时候不是很好,因为很有可能用户在输入多个EditView之后,不知道当前EditView需
要输入什么内容。为了解决这一问题,TextInputLayout就此诞生了。TextInputLayout是继承自LinearLayout容
器布局,因此我们需要将EditView包含在TextInputLayout之内才可以使用,言外之意:TextInputLayout不能单
独使用。废话不多说,先上效果图啊:
XML布局代码如下:
<android.support.design.widget.TextInputLayout android:id="@+id/textInput" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black"/> </android.support.design.widget.TextInputLayout>
代码也可以看出TextInputLayout包裹着EditView。
为了达到以上效果,我们还需添加如下代码:
final TextInputLayout inputLayout = findView(R.id.textInput); inputLayout.setHint("请输入姓名:"); EditText editText = inputLayout.getEditText(); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length()>4){ inputLayout.setErrorEnabled(true); inputLayout.setError("姓名长度不能超过4个"); }else{ inputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable s) { } });
TextInputLayout 不仅能让EditView的提示语上弹显示在EditView之上,而且还能把错误信息显示在EditView之下。
TextInputLayout常用的方法有如下:
- setHint():设置提示语。
- getEditText():得到TextInputLayout中的EditView控件。
- setErrorEnabled():设置是否可以显示错误信息。
- setError():设置当用户输入错误时弹出的错误信息。
注意点
- TextInputLayout不能单独使用,需要包裹EditView组件。
【转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46500865 CSDN 废墟的树】
0 0
- TextInputLayout控件
- 新控件之TextInputLayout
- TextInputLayout-Android M新控件
- EditText辅助控件—TextInputLayout
- Material Design控件之TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- TextInputLayout
- FloatingActionButton,TextInputLayout,Snackbar,TabLayout 四种控件
- Android 5.0新控件——TextInputLayout
- Android 5.0+ 解析(六)TextInputLayout控件
- Material Design控件之三TextInputLayout
- TextInputLayout输入框控件的悬浮标签
- 对象的生存与死亡
- Linked List Cycle
- 【POJ2367】
- 数据库事务ACID原则
- ffprobe使用详解
- TextInputLayout控件
- iOS进阶之UI利器Reveal(1)--Reveal的下载,安装和破解
- 批处理学习,持续更新。。。
- IBM发明世界首个人造神经元,人工智能的底层硬件基石已完成!
- JMeter学习(八)JDBC测试计划-连接Oracle
- hdu 5802——Windows 10
- C++ 设计一个不能被继承的类
- Ubuntu桌面
- Win7系统安装MySQL5.5.21图解教程大家都知道MySQL是一款中、小型关系型数据库管理系统,很具有实用性