本文系转载,原文地址http://blog.csdn.net/caihongdao123/article/details/51579366
hello,大家好!
今天给大家更新的博客是有关TextInputLayout的简单介绍。
TextInputLayout简介
TextInputLayout是把EditText作为自己子控件的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方。它解决了当用户点击EditText时Hint的文字消失了而导致用户可能不知道当前输入的内容是什么的问题。
TextInputLayout使用
1.导入依赖包
- dependencies {
- compile 'com.android.support:appcompat-v7:22.2.0'
- compile 'com.android.support:design:22.2.0'
- }
2.布局文件
- <android.support.design.widget.TextInputLayout
- android:layout_width="fill_parent"
- android:id="@+id/til_hint_content"
- app:errorEnabled="true"
- android:layout_height="wrap_content">
-
- <EditText android:id="@+id/et_hint_content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:saveEnabled="false"
- android:maxLength="48"
- android:digits="1234567890qwertyuiopasdfghjklzxcvbnm "
- android:hint="你的提示文字"/>
- </android.support.design.widget.TextInputLayout>
TextInputLayout添加逻辑判断
- et_hint_content.addTextChangedListener(new TextWatcher() {
- @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
-
- }
-
- //输入不符合设置的字符,hint会变成红色并提醒
- @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- //检查输入是否符合自定义规则
- if (checkType(charSequence.toString())) {
- til_hint_content.setErrorEnabled(true);
- til_hint_content.setError("输入信息有误");
- return;
- } else {
- til_hint_content.setErrorEnabled(false);
- }
- }
-
- @Override public void afterTextChanged(Editable editable) {
-
- }
- });