Android Design Support Library——TextInputLayout
2016-01-18 11:14
555 查看
[b]前沿[/b]
上一篇介绍了NavigationView的主要使用方式,本章主要介绍TextInputLayout的使用方式。
TextInputLayout——EditText悬浮标签
TextInputLayout主要作为EditText的父容器来使用,不能单独使用。TextInputLayout解决了EditText输入后hint文字消失的情况,当用户在EditText开始输入后,hint部分的文字会浮动到EditText的上方,而且还可以添加输入错误时的提示信息,显示在editText的下方,效果如下:
使用步骤:
(1)xml布局
(2)Java代码调用
此外可以通过TextInputLayout如下两个属性来设置hint和error的字体
/style/FloatingStyle
上一篇介绍了NavigationView的主要使用方式,本章主要介绍TextInputLayout的使用方式。
TextInputLayout——EditText悬浮标签
TextInputLayout主要作为EditText的父容器来使用,不能单独使用。TextInputLayout解决了EditText输入后hint文字消失的情况,当用户在EditText开始输入后,hint部分的文字会浮动到EditText的上方,而且还可以添加输入错误时的提示信息,显示在editText的下方,效果如下:
使用步骤:
(1)xml布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.TextInputLayout android:id="@+id/usernameWrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:inputType="textEmailAddress" /> </android.support.design.widget.TextInputLayout> </RelativeLayout>
(2)Java代码调用
final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.usernameWrapper); textInputLayout.setHint("Username"); EditText editText = textInputLayout.getEditText();//直接通过getEditText()获得EditText控件即可 editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (s.length() > 3) {//这里可以加入正则判断 textInputLayout.setError("Password error"); textInputLayout.setErrorEnabled(true); //一定要在setError方法之后执行才可 } else { textInputLayout.setErrorEnabled(false);//不满足条件需要设置为false } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } }); }
此外可以通过TextInputLayout如下两个属性来设置hint和error的字体
app:errorTextAppearance="@style/FloatingStyle" app:hintTextAppearance="@style/FloatingStyle"
/style/FloatingStyle
<style name="FloatingStyle" parent="@android:style/TextAppearance"> <item name="android:textColor">@color/colorPrimaryDark</item> <item name="android:textSize">12sp</item> </style>
相关文章推荐
- 手把手教你画一个 逼格满满圆形水波纹loadingview Android
- 手把手教你画一个 逼格满满圆形水波纹loadingview Android
- Android 历史版本
- Android学习之Animation(二)
- Android学习之Animation(二)
- Android设备修改host文件以及修改设备网关
- TabLayout的使用
- android加固系列—4.加固前先学会破解,无源码调试apk
- Android实现EditText内容保存为Bitmap的方法
- Android笔记
- 第一次写博客,先上传一下平常开发android的时候做的一点笔记
- Android 系统应用Setting开发总结
- Android无源码调试Native代码(gdb+IDA)
- focusableInTouchMode focusable
- Android 自定义 ViewPager 打造千变万化的图片切换效果
- Android数据存储——1,SharedPreferences使用共享偏好
- 常用Android开发组件之按钮类组件
- 阅读《Android 从入门到精通》(12)——自动完成文本框
- android 给界面加指定的字体
- Android数据存储简单介绍