1. 程式人生 > >TextInputLayout 和 TextInputEditText 的簡單介紹以及使用

TextInputLayout 和 TextInputEditText 的簡單介紹以及使用

watcher 信息 cal line led retext name sign 控件

TextInputLayout 和 TextInputEditText 是屬於 design 包裏面的控件

吶,就是這個:compile ‘com.android.support:design:26.0.0-alpha1‘

這兩者要結合使用,其實只用 TextInputLayout ,然後 TextInputEditText 用 EditText 替換好像也可以。

下面來看一個示例代碼咯

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/userNameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/passwdLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/passwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

</LinearLayout>

 Activity的代碼如下

    private TextInputLayout userNameLayout;
    private TextInputEditText userName;

    private TextInputLayout passwdLayout;
    private TextInputEditText passwd;

    userNameLayout = (TextInputLayout) findViewById(R.id.userNameLayout);
    userName = (TextInputEditText) findViewById(R.id.userName);
    userNameLayout.setHint("請輸入賬號");
    userName.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 (s.length() < 3) {
                userNameLayout.setErrorEnabled(true);
                userNameLayout.setError("用戶名不能小於3位");
            } else {
                userNameLayout.setErrorEnabled(false);
            }
        }
    });


    passwdLayout = (TextInputLayout) findViewById(R.id.passwdLayout);
    passwdLayout.setHint("請輸入密碼");
    passwd = (TextInputEditText) findViewById(R.id.passwd);
    passwd.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 (s.length() < 3) {
                passwdLayout.setErrorEnabled(true);
                passwdLayout.setError("密密不222能小於3位");
            } else {
                passwdLayout.setErrorEnabled(false);
            }
        }
    });

    }

其實也沒什麽,

主要的代碼也就幾行

userNameLayout.setHint("請輸入賬號");

這行代碼,就是設置提示信息

passwdLayout.setErrorEnabled(true);

這行表示,啟用錯誤提示,相應的,傳入false就表示關閉錯誤提示。

passwdLayout.setError("密密不222能小於3位");

這個表示,具體的錯誤提示

TextInputLayout 和 TextInputEditText 的簡單介紹以及使用