Android之TextInputLayout詳解

TextInputLayout是什麼

TextInputLayout主要是作爲EditText的容器,從而爲EditText生成一個浮動的Label,當用戶點擊EditText的時候,EditText中的hint字符串會自動移到EditText的左上角。

TextInputLayout如何使用

●基本用法

xml佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="請輸入用戶名"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="請輸入郵箱"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

●設置最大字符數及錯誤提示

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:counterEnabled="true" //設置爲true才能顯字符數
        app:counterMaxLength="5" //設置最大字符數爲5
        app:counterOverflowTextAppearance="@style/HintError" //設置超出字符數後提示文字的顏色,如果不設置默認爲@color/colorAccent的顏色
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="請輸入用戶名"
            android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>

style文件(設置超出字符數的文字提示顏色爲紅色)

<style name="HintError" parent="TextAppearance.AppCompat">
        <item name="android:textColor">@color/colorRed</item>
</style>

●設置錯誤提示文字

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:errorEnabled="true" //設置爲true
        android:id="@+id/textinputlayout_email"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="請輸入郵箱"
            android:id="@+id/et_email"
            android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>

java代碼

editText_email=findViewById(R.id.et_email);
        textInputLayout =findViewById(R.id.textinputlayout_email);
        editText_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!RegexUtils.isEmail(charSequence)){
                    textInputLayout.setError("郵箱格式錯誤");
                    textInputLayout.setErrorEnabled(true);
                }else {
                    textInputLayout.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

●設置密碼是否可見

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:errorEnabled="true"
        app:passwordToggleEnabled="true" //設置爲true
        android:id="@+id/textinputlayout_password"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="請輸入密碼"
            android:id="@+id/et_password"
            android:inputType="textPassword"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>




鏈接:https://www.jianshu.com/p/4c99e4c0fe90
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章