如何使用Android Design中的TextInputLayout

如题所述

TextInputLayout是一个能够把EditText包裹在当中的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方。

TextInputLayout is a layout which wraps an EditText to show a floating label when the hint is hidden due to the user inputting text. Also supports showing an error via setErrorEnabled(boolean) and [setError(CharSequence)](https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setError(java.lang.CharSequence).


效果如下

实现步骤

0x01. 添加依赖

dependencies {
 compile 'com.android.support:appcompat-v7:22.2.0'
 compile 'com.android.support:design:22.2.0'
}

0x02. UI代码

使用TextInputLayout包裹住EditText

<android.support.design.widget.TextInputLayout
         android:layout_width="fill_parent"
         android:id="@+id/your_matchcode_holder"
         app:errorEnabled="true"
         android:layout_height="wrap_content">

       <EditText android:id="@+id/your_matchcode"
           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>

0x03. 添加逻辑判断

在EditText中添加输入监听代码,注意在onTextChanged中调用才有实时效果

mYourMatchcode.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())) {
         mYourMatchcodeHolder.setErrorEnabled(true);
         mYourMatchcodeHolder.setError("请检查格式");
         return;
       } else {
         mYourMatchcodeHolder.setErrorEnabled(false);
       }
     }

     @Override public void afterTextChanged(Editable editable) {

     }
   });

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-03-13
实现步骤
1. 添加依赖
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
}

2. UI代码
使用TextInputLayout包裹住EditText
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:id="@+id/your_matchcode_holder"
app:errorEnabled="true"
android:layout_height="wrap_content">

<EditText android:id="@+id/your_matchcode"
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>

3. 添加逻辑判断
在EditText中添加输入监听代码,注意在onTextChanged中调用才有实时效果
mYourMatchcode.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())) {
mYourMatchcodeHolder.setErrorEnabled(true);
mYourMatchcodeHolder.setError("请检查格式");
return;
} else {
mYourMatchcodeHolder.setErrorEnabled(false);
}
}

@Override public void afterTextChanged(Editable editable) {

}
});本回答被提问者采纳
第2个回答  推荐于2018-04-20

    在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android 2.1 。 这些组件在我看来就是对 Github中最近比较火的 android 组件进行了封装!!

    要使用这个库,当然得先下载啦!( 没有FQ的同学肯定已经哭晕在厕所了!! )这里我给童鞋们,提供一个简单的方法:之后就是使用 Android SDK Manager 更新下载了!

    第一步--引用

    引用这个库:在 build.gradle 文件中加上这段代码: compile 

    第二步--具体控件的使用

    TextInputLayout

    以前当我们使用 EditText 这个组件的 hint 属性的时候,当用户输入第一个字母之后,这个 hint 中的文本就会消失!有点影响用户体验!但是现在不一样了!现在只要把 EditText 包含在 TextInputLayout 中,这个 hint 中的文本就会变成 floating lable 浮动在 EditText 上方!同时,也支持在 EditText 下方显示错误信息好了,我么先来看一下 TextInputLayout 这个控件的公共方法吧!

本回答被网友采纳
第3个回答  2017-07-30
用柱子撑了起来
相似回答