TextInputLayout输入框控件的悬浮标签
2016-09-22 21:18
281 查看
TextInputLayout也是5.0以后的效果,想要使用同样需要在build中配置:
dependencies { compile 'com.android.support:design:23.3.0' }TextInputLayout可以用来显示一个提示错误信息,把Hint放到EditText左上方等效果的一个布局;
如果项目中有这类的需求,使用TextInputLayout实现起来非常方便;
使用方法也比较简单,直接用TextInputLayout包裹EditText即可:
<android.support.design.widget.TextInputLayout android:id="@+id/til_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"> <EditText android:id="@+id/et_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> </android.support.design.widget.TextInputLayout>但是默认情况下,当你输入文本的时候TextInputLayout只会将Hint移动到左上方,不会有错误提示,错误提示需要我们手动设置:
etUser= (EditText) findViewById(R.id.et_user); tilUser= (TextInputLayout) findViewById(R.id.til_user); //添加文本变化监听 etUser.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()>6){ //打开TextInputLayout异常提示 tilUser.setErrorEnabled(true); //设置TextInputLayout异常提示信息 tilUser.setError("账号最大长度为6"); }else { //关闭TextInputLayout异常提示 tilUser.setErrorEnabled(false); } } @Override //输入以后调用 public void afterTextChanged(Editable s) { } });
点击打开链接免费下载源码
相关文章推荐
- TextInputLayout输入框控件的悬浮标签
- Android Design新控件之TextInputLayout(文本输入布局)
- Android 5.0新控件 TextInputLayout | 文字输入布局 介绍及使用详情
- Android Design新控件之TextInputLayout(文本输入布局)
- Android 5.0新控件——TextInputLayout
- Android M新控件之FloatingActionButton,TextInputLayout,Snackbar的使用
- Android开发笔记(一百三十八)文本输入布局TextInputLayout
- 强大的提示控件TextInputLayout使用以及源码分析
- FloatingActionButton,TextInputLayout,Snackbar,TabLayout 四种控件
- TextInputLayout控件
- Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
- 一天一个控件——TextInputLayout
- Android 5.0新控件——TextInputLayout
- Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
- 使用TextInputLayout提升EditText的输入用户体验
- Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
- JS获取input标签的text值、JS获取下拉框内容、input控制只能输入数字等问题汇总
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
- 强大的提示控件TextInputLayout使用以及源码分析
- Flex textinput, 可以输入自定义的item, 类似wow的输入框控件