hello,大家好!
今天给大家更新的博客是有关TextInputLayout的简单介绍。
TextInputLayout简介
TextInputLayout是把EditText作为自己子控件的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方。它解决了当用户点击EditText时Hint的文字消失了而导致用户可能不知道当前输入的内容是什么的问题。
TextInputLayout使用
1.导入依赖包
<span style="font-size:18px;">dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
}</span>
2.布局文件
<span style="font-size:18px;"><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></span>
TextInputLayout添加逻辑判断
<span style="font-size:18px;">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) {
}
});</span>
博客地址:http://blog.csdn.net/caihongdao123
下一篇 Android 5.0+ 解析(七)Snackbar控件