Android 輸入校驗庫:Fire Eye

jopen 9年前發布 | 16K 次閱讀 Fire Eye Android開發 移動開發

FireEye 2.0 在 1.0 的基礎上,全部重寫了代碼,并優化了架構,性能上和邏輯上都大大提升。


輕量級簡單易用的Android校驗庫。

這是一個簡單Android校驗庫,按配置來驗證用戶輸入的表單信息。只需要幾行代碼,即可驗證用戶輸入,并且將驗證錯誤反饋給用戶。它內置了大量常用的驗證類型,足以滿足你的功能需求。

截圖

Gradle 依賴

Add dependency

dependencies {
    compile 'com.github.yoojia:fire-eye:2.2@aar'
}

Maven
<dependency>
    <groupId>com.github.yoojia</groupId>
    <artifactId>fire-eye</artifactId>
    <version>2.2</version>
    <type>aar</type>
</dependency>

已內置支持的校驗方式

靜態模式 - StaticPattern

靜態模式是指對輸入內容進行模式匹配,不需要額外參數即可校驗的模式。如校驗郵件地址是否正確等。

  • Required 必填選項
  • NotBlank 非空數據
  • Digits 僅數字
  • Email 電子郵件
  • Numeric 數值
  • BankCard 信用卡號/銀行卡號
  • Host 主機地址
  • URL Http URL
  • IPv4 IPv4地
  • Mobile 中國的手機號碼
  • VehicleNumber 中國的民用車輛號牌
  • IDCard 中國的身份證號(15位和18位)

數值模式 - ValuePattern

數值模式是指需要額外參數來完成對輸入內容的校驗過程的模式。如判斷內容是否與另一個相同等。

  • EqualsTo 與指定值相同
  • NotEqualsTo 與指定值不相同
  • RangeLength 指定長度范圍
  • MinLength 最小長度
  • MaxLength 最大長度
  • RangeValue 最值范圍
  • MinValue 最小值
  • MaxValue 最大值

Usage - 如何使用

對表單內各個EditText綁定其校驗配置

// 自定義顯示出錯消息的方式,默認是在 EditText 右邊顯示一個浮動提示框。
MessageDisplay messageDisplay = new MessageDisplay() {
    @Override
    public void dismiss(TextView field) {
        field.setError(null);
    }

    @Override
    public void show(TextView field, String message) {
        field.setError(message);
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    }
};

// 使用表單查找器來查找輸入框
View formView = findViewById(R.id.form);
Form form = new Form(formView);

FireEye fireEye = new FireEye();
fireEye.add(form.byId(R.id.form_field_1), StaticPattern.Required.setMessage(R.string.tip_required), StaticPattern.Mobile);
fireEye.add(form.byId(R.id.form_field_2), StaticPattern.BankCard.setMessage("請輸入您的銀行卡號"));

fireEye.add(form.byId(R.id.form_field_3), StaticPattern.Digits);
fireEye.add(form.byId(R.id.form_field_3), ValuePattern.MaxLength.setValue(20));

fireEye.add(form.byId(R.id.form_field_4), StaticPattern.Required, StaticPattern.Email);
fireEye.add(form.byId(R.id.form_field_5), ValuePattern.Required, ValuePattern.EqualsTo.lazy(new TextViewLoader(form.byId(R.id.form_field_4))));
fireEye.add(form.byId(R.id.form_field_6), StaticPattern.Host);
fireEye.add(form.byId(R.id.form_field_7), StaticPattern.URL);
fireEye.add(form.byId(R.id.form_field_8), ValuePattern.MaxLength.setValue(5));
fireEye.add(form.byId(R.id.form_field_9), ValuePattern.MinLength.setValue(4));
fireEye.add(form.byId(R.id.form_field_10), ValuePattern.RangeLength.setFirstValue(4L).setSecondValue(8L));
fireEye.add(form.byId(R.id.form_field_11), StaticPattern.NotBlank);
fireEye.add(form.byId(R.id.form_field_12), StaticPattern.Numeric);
fireEye.add(form.byId(R.id.form_field_13), ValuePattern.MaxValue.setValue(100));
fireEye.add(form.byId(R.id.form_field_14), ValuePattern.MinValue.setValue(20));
fireEye.add(form.byId(R.id.form_field_15), ValuePattern.RangeValue.setFirstValue(18L).setSecondValue(30L));

Result r = eye.test();

if(r.passed){
    // 校驗通過
}else{
    // 校驗失敗
}

有用的接口

Debug

FireEyeEnv.isDebug = true;

設置FireEye環境變量,可以查看FireEye的校驗過程及結果。

DUMP

FireEye.dump()

此方法可以輸出詳細的校驗配置信息。其輸出內容示例:

android.support.v7.internal.widget.TintEditText{42a99d60 VFED..CL .F...... 0,0-1080,118 #7f090040 app:id/form_field_1}@必填選項|手機號碼:
-> patterns:
{pattern=Required, messageId=-1, message='請填寫您的手機號'} ,
{pattern=Mobile, messageId=-1, message='手機號錯誤'}

項目主頁:http://www.baiduhome.net/lib/view/home/1439454272411

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!