Android Design Support Library 使用详解二(TextInputLayout)
2017-03-08 17:27
393 查看
TextInputLayout作为一个父容器控件,包装了新的EditText。通常,单独的EditText会在用户输入第一个字母之后隐藏hint提示信息,但是现在你可以使用TextInputLayout 来将EditText封装起来,提示信息会变成一个显示在EditText之上的floating label,这样用户就始终知道他们现在输入的是什么。同时,如果给EditText增加监听,还可以给它增加更多的floating label。
下面我们来看这与一个TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="@+id/til_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>一定要注意,他是把EditText包含起来的,不能单独使用。
在代码中,我们给它设置监听:
final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.til_pwd);
EditText editText = textInputLayout.getEditText();
textInputLayout.setHint("Password");
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length() > 4) {
textInputLayout.setError("Password error");
textInputLayout.setErrorEnabled(true);
} else {
textInputLayout.setErrorEnabled(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
下面我们来看这与一个TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="@+id/til_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>一定要注意,他是把EditText包含起来的,不能单独使用。
在代码中,我们给它设置监听:
final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.til_pwd);
EditText editText = textInputLayout.getEditText();
textInputLayout.setHint("Password");
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length() > 4) {
textInputLayout.setError("Password error");
textInputLayout.setErrorEnabled(true);
} else {
textInputLayout.setErrorEnabled(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
相关文章推荐
- Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
- Android Design Support Library(2)- TextInputLayout的使用
- Android Design Support Library(2)- TextInputLayout的使用
- Android Design Support Library - TextInputLayout使用
- Android Design Support Library - TextInputLayout使用
- Android Design Support Library--TextInputLayout的使用
- Android Design Support Library(一)--TextInputLayout简析
- android_design support library_TextInputLayout
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- Android Design Support Library(一):FloatingActionButton、TextInputLayout、TextInputEditText简单用法
- Android Design Support Library——TextInputLayout
- Android Design Support Library使用详解
- Android Design Support Library使用详解
- Android Design Support Library使用详解