1. 程式人生 > >UI TextInputLayout 的簡單用法

UI TextInputLayout 的簡單用法

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation
="vertical">
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="請輸入使用者名稱" /> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height
="wrap_content" android:id="@+id/textInputLayout" >
<!-- app:counterOverflowTextAppearance="@color/colorPrimary" 超出字數另外的顏色--> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height
="wrap_content" android:hint="請輸入密碼" />
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:hintAnimationEnabled="false" > <!-- app:hintAnimationEnabled="false" 禁用掉動畫--> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入" /> </android.support.design.widget.TextInputLayout> </LinearLayout>
package com.example.bluetooth;

import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

public class TextInPutActivity extends AppCompatActivity {

    private TextInputLayout textInputLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_in_put);
//        new TextInputLayout()
        textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
        textInputLayout.setCounterEnabled(true);//開啟校驗字數功能
        textInputLayout.setCounterMaxLength(10);//設定最大的字數
        textInputLayout.getEditText().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 (textInputLayout.getEditText().getText().toString().length() == 0){
                    textInputLayout.setErrorEnabled(false);//關閉錯誤提示
                }else if(textInputLayout.getEditText().getText().toString().length() <= 6){
                    textInputLayout.setErrorEnabled(true);//開啟錯誤提示
                    textInputLayout.setError("低於6位數,不符合要求");//設定錯誤提示資訊
                }else{
                    textInputLayout.setErrorEnabled(false);//關閉錯誤提示
                }

            }
        });
        textInputLayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(v.hasFocus()){

                }else{
                    textInputLayout.setErrorEnabled(false);//關閉錯誤提示
                }
            }
        });

        //textInputLayout.setEnabled(false);//導致EditText無法點選,獲取不到焦點

    }
}