我的Material Design EditText,實(shí)現(xiàn)刪除功能

我的Material Design EditText,實(shí)現(xiàn)刪除功能

本文原創(chuàng),轉(zhuǎn)載請注明出處璃弄。歡迎關(guān)注我的 簡書夏块。
github:MyDemo喜歡的話就給個Star
安利一波我寫的開發(fā)框架:MyScFrame喜歡的話就給個Star

前言:

Material Design推出了一種優(yōu)秀的文本輸入框形式——當(dāng)用戶點(diǎn)擊空內(nèi)容的文本輸入框時,原本位于輸入框內(nèi)的提示語會經(jīng)由一個動畫浮動至輸入框上方浑塞,文本顏色同時變成強(qiáng)調(diào)色政己,明確顯示此時該組件獲得焦點(diǎn)
為了讓開發(fā)者更加方便地實(shí)現(xiàn)這種效果Google推出了TextInputLayout組件,里面放置一個EditText或者TextInputEditText,可以輕松實(shí)現(xiàn)

附上效果圖

大體效果如圖

添加依賴

compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'

TextInputLayout常用屬性以及用法

布局中的名稱 參數(shù)類型 代碼中的方法名稱 注解
counterEnabled boolean setCounterEnabled(boolean) 用于設(shè)置字符計數(shù)器的開啟或關(guān)閉
counterMaxLength int setCounterMaxLength(int) 設(shè)置字符計數(shù)器的最大長度。(僅用于設(shè)置計數(shù)器最大值仅孩,并不影響文本實(shí)際能輸入的最大長度)
errorEnabled boolean setErrorEnabled(boolean) 用于設(shè)置錯誤提示是否開啟辽慕。
hint String setHint(CharSequence) 設(shè)置輸入框的提示語。
hintAnimationEnabled boolean setHintAnimationEnabled(boolean) 開啟或關(guān)閉hint浮動成標(biāo)簽的動畫效果公浪。
hintEnabled boolean setHintEnabled(boolean) 開啟或關(guān)閉hint浮動的功能船侧,設(shè)為false的話就和之前的EditText一樣,在輸入文字后预柒,提示語就消失了袁梗。
hintTextAppearance style setHintTextAppearance(int) 設(shè)置hint的style遮怜,字體顏色,字體大小等锯梁,可引用系統(tǒng)自帶的也可以自定義陌凳。若要使用請統(tǒng)一使用,以保證APP體驗(yàn)的統(tǒng)一性蕊肥。
passwordToggleEnabled boolean setPasswordVisibilityToggleEnabled(boolean) 控制密碼可見開關(guān)是否啟用蛤肌。設(shè)為false則該功能不啟用批狱,密碼輸入框右側(cè)也沒有控制密碼可見與否的開關(guān)。
passwordToggleDrawable Drawable setPasswordVisibilityToggleDrawable(Drawable) 設(shè)置密碼可見開關(guān)的圖標(biāo)炒俱。通常我們會在不同的情況下設(shè)定不同的圖標(biāo),可通過自定義一個selector砸王,根據(jù)“state_checked”屬性來控制圖標(biāo)的切換峦阁。
passwordToggleTint Color setPasswordVisibilityToggleTintList(ColorStateList) 控制密碼可見開關(guān)圖標(biāo)的顏色榔昔。在開啟或關(guān)閉的狀態(tài)下我們可以設(shè)定不同的顏色,可通過自定義一個color的selector嘹朗,根據(jù)“state_checked”和“state_selected”屬性來控制顏色的切換诵肛。
passwordToggleTintMode PorterDuff.Mode setPasswordVisibilityToggleTintMode(PorterDuff.Mode) 控制密碼可見開關(guān)圖標(biāo)的背景顏色混合模式。這個地方我不是很能理解褪秀,希望有人可以指教珠洗。
passwordToggleContentDescription int setPasswordVisibilityToggleContentDescription(int) 該功能是為Talkback或其他無障礙功能提供的。TalkBack會去讀contentDescription的值蝴猪,告訴用戶這個操作是什么膊爪。

passwordToggleTintMode相關(guān)知識有興趣請參考Xfermode in android 其中有關(guān)于這方面概念的解釋米酬。

layout布局

<?xml version="1.0" encoding="utf-8"?>
<!--
TextInputLayout:
    app:hintEnabled="true"http://設(shè)置是否可以使用hint屬性,默認(rèn)是true
    app:hintAnimationEnabled="true"http://設(shè)置是否可以使用動畫加派,默認(rèn)是true
    app:hintTextAppearance="@style/hintAppearance"http://設(shè)置hint的文本屬性跳芳,改變hint文字的大小顏色等屬性
    app:counterEnabled="true"http://設(shè)置是否可以開啟計數(shù)器,默認(rèn)是false
    app:counterOverflowTextAppearance="@style/counterOverflowTextAppearance"http://設(shè)置計算器越位后的文字顏色和大小
    app:counterTextAppearance="@style/hintAppearance"http://設(shè)置正常情況下的計數(shù)器文字顏色和大小
    app:counterMaxLength="11"http://設(shè)置計算器的最大字?jǐn)?shù)限制
    app:errorEnabled="true"http://是否允許錯誤提示娄琉,默認(rèn)是true
    app:errorTextAppearance="@style/errorAppearance"http://錯誤提示的文字大小和顏色
    app:passwordToggleEnabled="true"http://設(shè)置是否顯示密碼眼睛,默認(rèn)是false
    app:passwordToggleDrawable="@mipmap/ic_launcher"http://自定義眼睛圖標(biāo)
    app:passwordToggleTint="@color/colorAccent"http://給眼睛著色
    app:passwordToggleTintMode="multiply"http://選擇著色模式,與passwordToggleTint一起用
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/widget_textinput_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/TextInputLayoutLineColor"
        app:counterEnabled="true"
        app:counterMaxLength="11"
        app:counterOverflowTextAppearance="@style/counterOverflowTextAppearance"
        app:errorTextAppearance="@style/errorAppearance"
        app:hintTextAppearance="@style/hintAppearance">

        <com.caihan.mydemo.widget.edittext.AutoCheckEditText
            android:id="@+id/widget_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/v2_et_del_image"
            android:drawableStart="@drawable/v2_register_phone"
            android:hint="請輸入用戶名"
            android:imeOptions="actionNext"
            android:inputType="number"
            android:singleLine="true"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/widget_textinput_layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/TextInputLayoutLineColor"
        app:counterEnabled="true"
        app:counterMaxLength="11"
        app:counterOverflowTextAppearance="@style/counterOverflowTextAppearance"
        app:errorTextAppearance="@style/errorAppearance"
        app:hintTextAppearance="@style/hintAppearance"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/colorAccent">

        <com.caihan.mydemo.widget.edittext.AutoCheckEditText
            android:id="@+id/widget_et2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/v2_login_pwd"
            android:hint="請輸入密碼"
            android:imeOptions="actionDone"
            android:inputType="textPassword"
            android:singleLine="true"/>
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

Style

<!--EditText的主題 Start-->
<style name="TextInputLayoutLineColor" parent="Theme.AppCompat.Light">
    <!--沒有獲取焦點(diǎn)時的顏色-->
    <item name="colorControlNormal">@color/colorAccent</item>
    <!--獲取焦點(diǎn)時的顏色-->
    <item name="colorControlActivated">@color/main_color</item>
</style>

<!--浮動標(biāo)簽-->
<style name="hintAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/colorPrimary</item>
</style>

<!--錯誤提示信息-->
<style name="errorAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@android:color/holo_red_light</item>
</style>

<!--超出計數(shù)最大長度,浮動標(biāo)簽,下劃線,計數(shù)文字都會改變顏色-->
<style name="counterOverflowTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@android:color/holo_red_dark</item>
</style>
<!--EditText的主題 End-->

模塊化

我喜歡吧一個一個功能或者幾個功能打包起來當(dāng)成一個模塊,然后項(xiàng)目由一個一個模塊組成,每個模塊之間是完全解耦的,這樣有利于后期的維護(hù)跟修改
先來看看結(jié)構(gòu)


AutoCheckEditText

WarnViewStatus

/**
 * Created by caihan on 2016/9/16.
 * 警告語顯示監(jiān)聽
 */
public interface WarnViewStatus {
    /**
     * 展示警告語
     *
     * @param msgs
     */
    void show(String... msgs);

    /**
     * 隱藏警告語
     */
    void hide();
}

WarningMsg

/**
 * Created by caihan on 2017/1/22.
 * 錯誤提示語
 */
public class WarningMsg {
    private static final String TAG = "WarningMsg";
    private int mType;
    private String mMsgString;

    public WarningMsg(int typ) {
        setType(typ);
    }

    private void setType(int typ) {
        mType = typ;
    }

    /**
     * 正則判斷錯誤的提示語
     * @return
     */
    public String getMsg() {
        switch (mType) {
            case EditTextType.TYPE_OF_MOBILE:
                //手機(jī)校驗(yàn)
                mMsgString = "請輸入正確手機(jī)號碼";
                break;
            case EditTextType.TYPE_OF_TEL:
                //座機(jī)校驗(yàn)
                mMsgString = "請輸入正確座機(jī)號碼";
                break;
            case EditTextType.TYPE_OF_EMAIL:
                //郵箱校驗(yàn)
                mMsgString = "請輸入正確郵箱";
                break;
            case EditTextType.TYPE_OF_URL:
                //url校驗(yàn)
                mMsgString = "請輸入正確地址";
                break;
            case EditTextType.TYPE_OF_CHZ:
                //漢字校驗(yàn)
                mMsgString = "請輸入正確中文";
                break;
            case EditTextType.TYPE_OF_USERNAME:
                //用戶名校驗(yàn)
                mMsgString = "請輸入正確用戶名";
                break;
            case EditTextType.TYPE_OF_USER_DEFINE:
                mMsgString = "";
                break;
            default:
                mMsgString = "";
                break;
        }
        return mMsgString;
    }

    /**
     * 不符合長度要求的提示語
     *
     * @return
     */
    public String getLengthMsg() {
        mMsgString = "不符合要求";
        return mMsgString;
    }

    public String getMinLengthMsg() {
        mMsgString = "不符合要求";
        return mMsgString;
    }

    public String getMaxLengthMsg() {
        mMsgString = "不符合要求";
        return mMsgString;
    }
}

EditTextType

/**
 * Created by caihan on 2017/1/22.
 */
public interface EditTextType {

    //手機(jī)校驗(yàn)類型
    int TYPE_OF_MOBILE = 0xb0;
    //座機(jī)校驗(yàn)類型
    int TYPE_OF_TEL = 0xb1;
    //郵箱校驗(yàn)類型
    int TYPE_OF_EMAIL = 0xb2;
    //url校驗(yàn)類型
    int TYPE_OF_URL = 0xb3;
    //漢字校驗(yàn)類型
    int TYPE_OF_CHZ = 0xb4;
    //用戶名校驗(yàn)類型
    int TYPE_OF_USERNAME = 0xb5;
    //用戶自定義
    int TYPE_OF_USER_DEFINE = 0xbb;
}

Check

/**
 * Created by caihan on 2017/1/16.
 * 正則判斷
 */
public class Check {
    private static final String TAG = "Check";
    //驗(yàn)證座機(jī)號,正確格式:xxx/xxxx-xxxxxxx/xxxxxxxx
    private static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";

    public static boolean getMatch(int typ, String string, String... userRegxs) {
        boolean isMatch = false;
        switch (typ) {
            case EditTextType.TYPE_OF_MOBILE:
                isMatch = isMobile(string);
                break;
            case EditTextType.TYPE_OF_TEL:
                isMatch = isTel(string);
                break;
            case EditTextType.TYPE_OF_EMAIL:
                isMatch = isEmail(string);
                break;
            case EditTextType.TYPE_OF_URL:
                isMatch = isURL(string);
                break;
            case EditTextType.TYPE_OF_CHZ:
                isMatch = isChz(string);
                break;
            case EditTextType.TYPE_OF_USERNAME:
                isMatch = isUsername(string);
                break;
            case EditTextType.TYPE_OF_USER_DEFINE:
                if (userRegxs != null && userRegxs.length > 0 && !StringUtils.isEmpty(userRegxs[0])) {
                    isMatch = isMatch(userRegxs[0], string);
                }
                break;
            default:
                break;
        }
        return isMatch;
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合手機(jī)號格式
     */
    private static boolean isMobile(String string) {
        return RegexUtils.isMobileSimple(string);
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合座機(jī)號碼格式
     */
    private static boolean isTel(String string) {
        return RegexUtils.isMatch(REGEX_TEL, string);
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合郵箱格式
     */
    private static boolean isEmail(String string) {
        return RegexUtils.isEmail(string);
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合網(wǎng)址格式
     */
    private static boolean isURL(String string) {
        return RegexUtils.isURL(string);
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合漢字
     */
    private static boolean isChz(String string) {
        return RegexUtils.isZh(string);
    }

    /**
     * @param string 待驗(yàn)證文本
     * @return 是否符合用戶名
     */
    private static boolean isUsername(String string) {
        return RegexUtils.isUsername(string);
    }

    /**
     * 判斷是否匹配正則
     *
     * @param regex 正則表達(dá)式
     * @param input 要匹配的字符串
     * @return {@code true}: 匹配<br>{@code false}: 不匹配
     */
    private static boolean isMatch(String regex, CharSequence input) {
        return RegexUtils.isMatch(regex, input);
    }
}

上面這些類基本上都沒什么可說的,我不習(xí)慣把所有東西都放在一個類里面,所以分了好多個類出來,個人習(xí)慣問題.
下面開始說下AutoCheckEditText這個類實(shí)現(xiàn)了刪除按鈕正則判斷
注解都寫的比較詳細(xì),就不重復(fù)說明了,如果有不懂的地方再問我

/**
 * Created by caihan on 2017/1/16.
 */
public class AutoCheckEditText extends TextInputEditText implements TextWatcher, View.OnFocusChangeListener {
    private static final String TAG = "AutoCheckEditText";

    private Context mContext;
    private int mType;
    private Drawable successDrawable;
    private Drawable unsuccessDrawable;
    private String userRegx;
    //左邊圖標(biāo)
    private Drawable mLeftDrawable;
    //右側(cè)刪除圖標(biāo)
    private Drawable mRightDrawable;
    private final int DRAWABLE_LEFT = 0;
    private final int DRAWABLE_TOP = 1;
    private final int DRAWABLE_RIGHT = 2;
    private final int DRAWABLE_BOTTOM = 3;
    private WarnViewStatus mWarnViewListener;
    private WarningMsg mWarningMsg;
    private int mMinLength = 0;
    private int mMaxLength = Integer.MAX_VALUE;

    public AutoCheckEditText(Context context) {
        super(context);
        init(context);
    }

    public AutoCheckEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AutoCheckEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        if (context == null) return;
        mContext = context;
        mRightDrawable = getCompoundDrawablesRelative()[DRAWABLE_RIGHT];
        mLeftDrawable = getCompoundDrawablesRelative()[DRAWABLE_LEFT];
//        this.setImeOptions(EditorInfo.IME_ACTION_DONE);
//        this.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        setCompoundDrawablesRelativeWithIntrinsicBounds(mLeftDrawable, null, null, null);
        this.addTextChangedListener(this);
        this.setOnFocusChangeListener(this);
    }

    /**
     * @param typ            要校驗(yàn)的類型
     * @param warnViewStatus 錯誤提示語狀態(tài)監(jiān)聽
     */
    public void creatCheck(int typ, WarnViewStatus warnViewStatus) {
        this.mType = typ;
        mWarningMsg = new WarningMsg(typ);
        mWarnViewListener = warnViewStatus;
    }

    /**
     * 設(shè)置判斷的長度區(qū)間
     *
     * @param minLength
     * @param maxLength
     */
    public void setLength(int minLength, int maxLength) {
        setMinLength(minLength);
        setMaxLength(maxLength);
    }

    public void setMinLength(int minLength) {
        mMinLength = minLength;
    }

    public void setMaxLength(int maxLength) {
        mMaxLength = maxLength;
    }

    /**
     * @param typ       要校驗(yàn)的類型
     * @param success   匹配成功時的圖標(biāo)
     * @param unsuccess 匹配失敗時的圖標(biāo)
     * @param userRegex 用戶自定義正則
     */
    public void creatCheck(int typ, Drawable success, Drawable unsuccess, String userRegex) {
        creatCheck(typ, success, unsuccess);
        this.userRegx = userRegex;
    }

    /**
     * @param typ       要校驗(yàn)的類型
     * @param success   匹配成功時的圖標(biāo)
     * @param unsuccess 匹配失敗時的圖標(biāo)
     */
    private void creatCheck(int typ, Drawable success, Drawable unsuccess) {
        mType = typ;
        successDrawable = success;
        successDrawable.setBounds(0, 0,
                successDrawable.getMinimumWidth(), successDrawable.getMinimumHeight());
        unsuccessDrawable = unsuccess;
        unsuccessDrawable.setBounds(0, 0,
                unsuccessDrawable.getMinimumWidth(), unsuccessDrawable.getMinimumHeight());
        mWarningMsg = new WarningMsg(typ);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        updateCleanable(s.toString().length(), true);
        if (s != null && s.length() > 0) {
            if (s.length() >= mMinLength && s.length() <= mMaxLength) {
                boolean isMatch = Check.getMatch(mType, s.toString(), userRegx);
                changeWarnStatus(!isMatch, mWarningMsg.getMsg());
//            if (isMatch) {
//                setCompoundDrawables(null, null, successDrawable, null);
//            } else {
//                setCompoundDrawables(null, null, unsuccessDrawable, null);
//            }
            } else {
                changeWarnStatus(true, mWarningMsg.getLengthMsg());
            }
        } else {
//            setCompoundDrawables(null, null, null, null);
            changeWarnStatus(false, mWarningMsg.getMsg());
        }
    }

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //可以獲得上下左右四個drawable,右側(cè)排第二孽水。圖標(biāo)沒有設(shè)置則為空票腰。
        if (mRightDrawable != null && event.getAction() == MotionEvent.ACTION_UP) {
            //檢查點(diǎn)擊的位置是否是右側(cè)的刪除圖標(biāo)
            //注意,使用getRwwX()是獲取相對屏幕的位置女气,getX()可能獲取相對父組件的位置
            int leftEdgeOfRightDrawable = getRight() - getPaddingRight()
                    - mRightDrawable.getBounds().width();
            if (event.getRawX() >= leftEdgeOfRightDrawable) {
                setText("");
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        mRightDrawable = null;
        super.finalize();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //更新狀態(tài)杏慰,檢查是否顯示刪除按鈕
        updateCleanable(this.getText().length(), hasFocus);
    }

    /**
     * 當(dāng)內(nèi)容不為空,而且獲得焦點(diǎn)主卫,才顯示右側(cè)刪除按鈕
     * @param length
     * @param hasFocus
     */
    private void updateCleanable(int length, boolean hasFocus) {
        if (length > 0 && hasFocus) {
            setCompoundDrawablesRelativeWithIntrinsicBounds(mLeftDrawable, null, mRightDrawable, null);
        } else {
            setCompoundDrawablesRelativeWithIntrinsicBounds(mLeftDrawable, null, null, null);
        }
    }

    /**
     * 更新錯誤提示語狀態(tài)
     *
     * @param isShow 是否顯示提示語,true = 顯示
     * @param msg    提示語
     */
    private void changeWarnStatus(boolean isShow, String msg) {
        if (mWarnViewListener != null) {
            if (isShow) {
                mWarnViewListener.show(msg);
            } else {
                mWarnViewListener.hide();
            }
        }
    }
}

可能你們會問,為什么這邊是繼承TextInputEditText而不是EditText?其實(shí)這是為了全屏模式時依然在編輯器里展示hint.這點(diǎn)我在前言里面有提到,這邊再次說明下,大家也可以用EditText代替

AutoCheckEditTextClass

接下來就是如何使用了,我這邊專門寫了個類,用來處理TextInputLayout與EditText

/**
 * Created by caihan on 2017/1/22.
 * 專門處理EditText的類
 * 請使用widget_autocheck_et_layout.xml
 */
public class AutoCheckEditTextClass implements WarnViewStatus {
    private static final String TAG = "AutoCheckEditTextClass";

    private TextInputLayout mTextInputLayout;
    private AutoCheckEditText mAutoCheckEditText;

    public AutoCheckEditTextClass(TextInputLayout inputLayout, AutoCheckEditText editText) {
        mTextInputLayout = inputLayout;
        mAutoCheckEditText = editText;
    }

    /**
     * 設(shè)置正則校驗(yàn)類型
     * @param typ
     * @return
     */
    public AutoCheckEditTextClass checkType(int typ) {
        mAutoCheckEditText.creatCheck(typ, this);
        return this;
    }

    /**
     * 設(shè)置最小判斷長度(一般不設(shè)置,默認(rèn)0)
     * @param minLength
     * @return
     */
    public AutoCheckEditTextClass setMinLength(int minLength) {
        mAutoCheckEditText.setMinLength(minLength);
        return this;
    }

    /**
     * @param maxLength      設(shè)置最大長度的時候,一并設(shè)置計算器的最大字?jǐn)?shù)限制
     * @param counterEnabled 計算器是否開啟
     * @return
     */
    public AutoCheckEditTextClass setMaxLength(int maxLength, boolean counterEnabled) {
        mTextInputLayout.setCounterMaxLength(maxLength);
        mTextInputLayout.setCounterEnabled(counterEnabled);
        mAutoCheckEditText.setMaxLength(maxLength);
        return this;
    }

    /**
     * @param maxLength 設(shè)置最大長度的時候,一并設(shè)置計算器的最大字?jǐn)?shù)限制
     * @return
     */
    public AutoCheckEditTextClass setMaxLength(int maxLength) {
        mTextInputLayout.setCounterMaxLength(maxLength);
        mTextInputLayout.setCounterEnabled(false);
        mAutoCheckEditText.setMaxLength(maxLength);
        return this;
    }

    /**
     * TextInputLayout hint開關(guān)
     * 如果只想用EditText默認(rèn)效果的話,請傳false,默認(rèn)是true
     *
     * @param hintEnabled
     * @return
     */
    public AutoCheckEditTextClass setHintEnabled(boolean hintEnabled) {
        mTextInputLayout.setHintEnabled(hintEnabled);
        return this;
    }

    @Override
    public void show(String... msgs) {
        mTextInputLayout.setErrorEnabled(true);
        if (msgs.length > 0 && !StringUtils.isEmpty(msgs[0])) {
            mTextInputLayout.setError(msgs[0]);
        }
    }

    @Override
    public void hide() {
        mTextInputLayout.setErrorEnabled(false);
    }
}

界面調(diào)用

好了,模塊就介紹到這里,最后讓我們來看看界面上是如何調(diào)用的

Layout里

就一句話

<include layout="@layout/widget_autocheck_et_layout"/>

Acticity里

也是只有一個鏈路

AutoCheckEditTextClass editTextClass = new AutoCheckEditTextClass(mWidgetTextinputLayout,
                mWidgetEt)
                .checkType(EditTextType.TYPE_OF_MOBILE)
                .setMaxLength(11,true);

搞定收工!


感謝

感謝Blankj 提供的工具類AndroidUtilCode以及帶正則校驗(yàn)的EditText

最后

一直想說找時間寫幾個Demo放到GitHub上,這樣比較方便伸手黨直接下載下來使用,不過一直沒時間,萬惡的老板開會說不能讓我太清閑,程序猿什么時候清閑過,大家評評理=.=

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市簇搅,隨后出現(xiàn)的幾起案子完域,更是在濱河造成了極大的恐慌,老刑警劉巖瘩将,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吟税,死亡現(xiàn)場離奇詭異,居然都是意外死亡姿现,警方通過查閱死者的電腦和手機(jī)肠仪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來备典,“玉大人异旧,你說我怎么就攤上這事√嵊叮” “怎么了吮蛹?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拌屏。 經(jīng)常有香客問我潮针,道長,這世上最難降的妖魔是什么倚喂? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任每篷,我火速辦了婚禮,結(jié)果婚禮上端圈,老公的妹妹穿的比我還像新娘焦读。我一直安慰自己,他們只是感情好舱权,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布吨灭。 她就那樣靜靜地躺著,像睡著了一般刑巧。 火紅的嫁衣襯著肌膚如雪喧兄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天啊楚,我揣著相機(jī)與錄音吠冤,去河邊找鬼。 笑死恭理,一個胖子當(dāng)著我的面吹牛拯辙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播颜价,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涯保,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了周伦?” 一聲冷哼從身側(cè)響起夕春,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎专挪,沒想到半個月后及志,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡寨腔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年速侈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迫卢。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡倚搬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出乾蛤,到底是詐尸還是另有隱情每界,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布幻捏,位于F島的核電站盆犁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏篡九。R本人自食惡果不足惜谐岁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望榛臼。 院中可真熱鬧伊佃,春花似錦、人聲如沸沛善。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽金刁。三九已至帅涂,卻和暖如春议薪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背媳友。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工斯议, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人醇锚。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓哼御,卻偏偏與公主長得像,于是被迫代替她去往敵國和親焊唬。 傳聞我的和親對象是個殘疾皇子恋昼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容