Material Design之TextInputLayout使用示例

使用TextInputLayout建立一个登录界面

1.导入支持库 html

使用TextInputLayout 控件须要导入两个库,一个是appcompat-v7,保证material styles能够向下兼容。另外一个是Design Support Library。 android

在项目的build.gradle文件中,添加下面的依赖(dependencies):web

dependencies {
        compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
        compile ‘com.android.support:design:22.2.0‘
        compile ‘com.android.support:appcompat-v7:22.2.0‘
    }

2.使用TextInputLayout app

TextInputLayout 控件表现得就像LinearLayout 同样,它是一个容器。TextInputLayout 中只能放一个子元素,和ScrollView有点相似,而且子元素必须是EditText 。字体

<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:inputType="textEmailAddress"
        android:hint="Username"/>

</android .support.design.widget.TextInputLayout>

注意到了么,我在EditText设置一个属性-hint。这个属性你们都很熟悉了,EditText没有输入的时候,hint会显示,当输入第一个字母上去的时候,hint就消失了。这个体验不是太好。 
感谢TextInputLayout,这个立刻就不是问题了。当EditText中输入第一个字母要隐藏hint的时候,TextInputLayout中会出现一个悬浮的标签来显示这个hint,还负责一个炫酷的的material 动画。
gradle

注:EditText的高度能够固定, TextInputLayout 的高度不要固定,不然TextInputLayout 的setError()的信息可能会没法正常显示动画

3.app:hintTextAppearance="@style/FloatingStyle"ui

app:hintTextAppearance=”@style/FloatingStyle” 用于设置floating字体的样式。spa

<style name="FloatingStyle" parent="@android:style/TextAppearance">
        <item name="android:textColor">#e0ffffff</item>
        <item name="android:textSize">12sp</item>
  </style>

TextInputLayout能够使用setError()方法在输入框下方显示错误信息,用途相似EditText的setError()。一样的,在xml中能够使用app:errorTextAppearance来设置错误信息的字体样式。code

 

 

参考连接:http://www.mamicode.com/info-detail-965904.html

其余连接:http://www.open-open.com/lib/view/open1433496206666.html

相关文章
相关标签/搜索