Android Design TextinputLayout

本文介绍了一个基于Android平台的登录界面实现方法,详细展示了如何使用TextInputLayout组件进行输入验证,并通过EditText进行用户邮箱及密码的输入。

 

使用该布局 需要在build.gradle中的dependencies块中添加两个依赖来向下兼容

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
}
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.alan.blogs.LoginActivity"
    tools:showIn="@layout/activity_login"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/email_TextInputLayout"
        android:layout_marginTop="32dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/emailHint"
            android:ems="10"
            android:id="@+id/edit_email" />
       </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password_TextInputLayout">
        <EditText
            android:inputType="textPassword"
            android:id="@+id/edit_password"
            android:hint="@string/passwordHint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </android.support.design.widget.TextInputLayout>
    <Button
        android:layout_marginTop="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/login"
        android:textColor="@color/white"
        android:background="@xml/ripple"
        />
</LinearLayout>

 

public void init(){
        this.emailInputLayout=(TextInputLayout)findViewById(R.id.email_TextInputLayout);
        this.passwordInputLayout=(TextInputLayout)findViewById(R.id.password_TextInputLayout);
       
        final EditText emailEditText=emailInputLayout.getEditText();
        EditText passwordEditText=passwordInputLayout.getEditText();
//        emailInputLayout.setHint("请输入Email");
//        passwordInputLayout.setHint("输入密码");                //也可以在editText中设置Hint InputLayput会自动获取该Hint
        emailEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(emailEditText.getText().toString().contains("@")==false){                    emailInputLayout.setErrorEnabled(true);                      emailInputLayout.setError("请输入正确的Email地址");                }else {
                    emailInputLayout.setErrorEnabled(false);
                }
            }
        });
    }

 

转载于:https://www.cnblogs.com/zzw1994/p/5238955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值