Android Support Design - TextInputLayout
2015-12-06 23:05
459 查看
TextInputLayout作为一个父容器控件,包装了新的EditText。通常,单独的EditText会在用户输入第一个字母之后隐藏 hint提示信息,但是现在你可以使用TextInputLayout 来将EditText封装起来,提示信息会变成一个显示在EditText之上的floating label,这样用户就始终知道他们现在输入的是什么。
注意
- TextInputLayout不能单独使用,需要包裹EditView组件。
- TextInputLayout的一些属性可以通过xml来设置,例如
- hint可以设置给EditText,与TextInputLayout.setHint()设置同样效果.
- error信息只能设置给与TextInputLayout,若设置给EditText,TextInputLayout没有任何变化
- 如果使用
style.xml
app:counterMaxLength=”10”
api url:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
注意
- TextInputLayout不能单独使用,需要包裹EditView组件。
- TextInputLayout的一些属性可以通过xml来设置,例如
app:errorEnabled="true"
- hint可以设置给EditText,与TextInputLayout.setHint()设置同样效果.
- error信息只能设置给与TextInputLayout,若设置给EditText,TextInputLayout没有任何变化
- 如果使用
app:errorEnabled="true"来开启error显示,EditText下方将会出现error TextView的占位。所以建议在需要显示error时使用代码开启,用完关闭.
设置hint样式
layout.xml<android.support.design.widget.TextInputLayout android:id="@+id/til_name" app:errorEnabled="true" app:hintTextAppearance="@style/ErrorText" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的姓名"/> </android.support.design.widget.TextInputLayout>
style.xml
<style name="ErrorText" parent="TextAppearance.AppCompat.Small"> <item name="android:textStyle">bold|italic</item> <item name="android:textColor">#00ff00</item> </style>
设置字数统计-Counter
app:counterEnabled=”true”app:counterMaxLength=”10”
<android.support.design.widget.TextInputLayout app:counterEnabled="true" app:counterMaxLength="10" android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的邮箱"/> </android.support.design.widget.TextInputLayout>
设置Error显示
til_mail= (TextInputLayout) findViewById(R.id.til_mail); et_mail=til_mail.getEditText(); et_mail.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { //开启错误显示 til_mail.setErrorEnabled(true); //设置错误显示 til_mail.setError("Error hint,你没有权限输入。"); til_mail.setErrorEnabled(false); } });
api url:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories