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无法点击,获取不到焦点

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39413778/article/details/82662280