Android Design Support之Floating labels for editing text实战

参考:https://android-developers.blogspot.com/2015/05/android-design-support-library.html


1、概述

这次我们来讲一个Design Support里面的一个组件:TextInputEditText。

我们都用过EditText,其有一个hint属性,就是能够提供提示信息。当我们输入字符的时候,这个hint就消失了。而TextInputEditText组件就能做到在输入字符的时候在上面显示出hint提示信息,非常方便。这个组件是继承自EditText。达到的效果如下所示:



2、使用

下面我们来看下怎么使用。

这个控件使用比较简单,直接看布局就行,当然首先需要在dependencies里面加入:

compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.easyliu.demo.floatinglabelsforedittextdemo.MainActivity">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="input"
            android:hint="@string/form_username" />

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

1、TextInputEditText必须放在TextInputLayout布局里面才能显示出效果。

2、同时上面显示出的hint的颜色默认是主题的colorAccent颜色。

3、然后记得使用兼容的Material Design主题即可,关于Material Design主题,请看之前的博文。

4、TextInputEditText还有几个自定义的属性,具体可以查看相关文档。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值