什么是TextInputLayout
Layout which wraps an {@link android.widget.EditText} (or descendant) to show a floating label when the hint is hidden due to the user inputting text.
意思就是:一个布局包裹了EditText(或者EditText的子类),在用户输入文本的时候显示一个浮动的提示标签。
TextInputLayout继承关系如下:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.LinearLayout
↳ android.support.design.widget.TextInputLayout
TextInputLayout的好处
传统的EditText,在用户输入信息的时,hint(提示)会自动消失,就如上图的中的第一行所示,这时候用户可能会忘记自己需要输入的内容是什么了,得删除所有的输入内容才可以看到hint的提示内容,这样的用户体验不太好。
而图二在用户输入的时候,hint会默认弹到上方,并且携带一个动画效果。它仅仅只是在EditText外包裹了一层TextInputLayout,然而就是这样一个小小的改动不仅提高了与用户交互的体验,而且解决了hint输入后消失的问题,唯一的不好就是“占地面积”大了一点,当然这一点点小瑕疵还是可以忽略的。
TextInputLayout的基本用法
1.首先要引入design和appcompat-v7兼容包:
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
2.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="tr