android中判断控件是否为空的地方非常之多,比如登录注册,只要是需要填写东西的地方基本上都需要判空。
难度不大,但是代码相对冗杂,如果集成框架的话就相对简单多了。
1.添加依赖
代码语言:javascript复制compile 'com.mobsandgeeks:android-saripaar:2.0.3'
2.布局
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="@ id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
......
<EditText
android:id="@ id/et6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/button" />
</LinearLayout>
3.获取对象并设置相应判断类型
代码语言:javascript复制 @BindView(R.id.button)
Button button;
@BindView(R.id.et1)
@NotEmpty(messageResId = R.string.errorMessage)//类型
@Order(1)//第几个
EditText et1;
@BindView(R.id.et2)
@NotEmpty(messageResId = R.string.errorMessage)
@Order(2)
EditText et2;
strings.xml
代码语言:javascript复制<resources>
<string name="app_name">ValidationDemo</string>
<string name="errorMessage">不能为空</string>
</resources>
4.onCreate中初始化
代码语言:javascript复制validator = new Validator(this);
validator.setValidationListener(this);
两个监听方法
代码语言:javascript复制 @Override
public void onValidationSucceeded() {//不为空的时候执行
//do somethings
//sendSubmit();
}
@Override
public void onValidationFailed(List<ValidationError> errors) {//为空的时候执行
for (ValidationError error : errors) {
View view = error.getView();
String message = error.getCollatedErrorMessage(this);
if (view instanceof EditText) {
((EditText) view).setError(message);
}
}
}
5.调用
代码语言:javascript复制 @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
validator.validate();
break;
}
}
6.扩展(非空、邮箱等)
NotEmpty @NotEmpty(messageId = R.string.validation_name, order = 1) private EditText mNameEditText;
MaxLength MinLength
@MinLength(value = 1, messageId = R.string.validation_participants, order = 2) private EditText mNameEditText;
MaxValue MinValue
@MinValue(value = 2L, messageId = R.string.validation_name_length) private EditText mEditNumberOfParticipants;
MaxNumberValue MinNumberValue
@MinNumberValue(value = "5.5", messageId = R.string.validation_name_length) private EditText mEditPotentialOfHydrogen;
RegExp
@RegExp(value = EMAIL, messageId = R.string.validation_valid_email) private EditText mEditEmail; @RegExp(value = "^[0-9] $", messageId = R.string.validation_valid_count) private EditText mEditCount;
DateInFuture
@DateInFuture(messageId = R.string.validation_date) private TextView mTxtDate;
DateNoWeekend
@DateNoWeekend(messageId = R.string.validation_date_weekend) private TextView mTxtDate;
Custom
@Custom(value = MyVeryOwnValidator.class, messageId = R.string.validation_custom) private EditText mNameEditText;
Demo地址:https://github.com/yechaoa/ValidationDemo
github:https://github.com/ragunathjawahar/android-saripaar/