利用TextInputLayout实现hint浮动显示(二)

来源:互联网 发布:python怎么发音 编辑:程序博客网 时间:2024/06/05 19:12
  • 学习平台:Android Studio2.2.3(基于win8.1 Pro)
  • ADT:Nexus 5
  • API :24
    文章参考自:Creating a Login Screen Using TextInputLayout
    先上最终效果图:
    这里写图片描述

接下来是步骤:
1. 新建TextInputLayoutDemo项目
New Project,主activity:LoginActivity,
Layout为login_layout。

2. 添加支持的Library
File–>Project Structure–>app–>添加依赖库。
com.android.support:design:25.0.1
这里写图片描述

3. 设计Login界面
这里写图片描述
显示一张图片和一个Welcome,分别靠ImageView和TextView实现 。
两个EditText,分别用于Username和Password。
还有一个用于触发登陆的Button。
下面是login_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:background="#e3e3e3"    android:orientation="vertical"    android:padding="@dimen/activity_horizontal_margin"    tools:context=".LoginActivity">    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@drawable/mylogo"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0.5"        android:orientation="vertical">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:gravity="center"            android:text="Welcome"            android:textColor="#333333"            android:textSize="30sp" />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0.5"        android:orientation="vertical">        <android.support.design.widget.TextInputLayout            android:id="@+id/username_wrapper"            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>        <android.support.design.widget.TextInputLayout            android:id="@+id/password_wrapper"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/username_wrapper"            android:layout_marginTop="4dp">            <EditText                android:id="@+id/password"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:hint="Password"                android:inputType="textPassword" />        </android.support.design.widget.TextInputLayout>        <Button            android:id="@+id/button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:text="Login" />    </LinearLayout></LinearLayout>

实际上,我做到这一步已经实现了hint的浮动显示,不过原文中还有一步,可能由于我使用的系统恰好可以,但是并非所有的系统到这一步就可以的。所以继续下一步。

4.设置Hints
首先,在setContentView方法下面初始化TextInputLayout视图额引用。
然后,使用setHint方法设置一个hint。
下面是代码:

   @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.login_layout);        final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.username_wrapper);        final TextInputLayout passwordWrapper = (TextInputLayout) findViewById(R.id.password_wrapper);        usernameWrapper.setHint("Username");        passwordWrapper.setHint("Password");    }

至此,TextInputLayout实现hint浮动显示就可以了。原文中还有关于条件验证的,我下一篇再继续。
谢谢!

0 0