前言
平時(shí)我們寫代碼比如登錄注冊(cè),在調(diào)接口之前肯定要檢查手機(jī)號(hào)啊晕粪,密碼啊是否規(guī)范挤悉,本文稱為內(nèi)容檢查。大多數(shù)人可能就是用if判斷一大串巫湘,我也是装悲,但是現(xiàn)在很煩,很low尚氛,所以想優(yōu)化一下這種代碼诀诊。
mDataBing.btnRegister.setOnClickListener(v -> {
String phoneNum = mDataBing.edtPhone.getText().toString().trim();
String password = mDataBing.edtPassword.getText().toString().trim();
if (TextUtils.isEmpty(phoneNum)) {
toast("手機(jī)號(hào)不能為空");
return;
}
if (RegexUtils.isMobileExact(phoneNum)) {
toast("手機(jī)號(hào)不正確");
return;
}
if (TextUtils.isEmpty(password)) {
toast("密碼不能為空");
return;
}
if (password.length() < 6) {
toast("密碼不能少于6位");
return;
}
//注冊(cè)
register();
});
有的app非空可能不會(huì)檢查,而是當(dāng)輸入內(nèi)容為空時(shí)讓提交按鈕不可點(diǎn)擊阅嘶。也就是當(dāng)所有的EditText有內(nèi)容時(shí)属瓣,按鈕才可點(diǎn)擊。我也寫了個(gè)工具類:
/**
*
* @author XuNeverMore
* @QQ 1045530120
* @create on 2018/2/2 0002
* @github https://github.com/XuNeverMore
*/
public class SubmitUtil implements TextWatcher {
private Button btnSubmit;//提交按鈕
private EditText[] editTexts;//需要填寫內(nèi)容的輸入框
private SubmitUtil() {
}
public SubmitUtil(Button btnSubmit, EditText... editTexts) {
this.btnSubmit = btnSubmit;
this.editTexts = editTexts;
}
public static SubmitUtil newInstance(){
return new SubmitUtil();
}
public SubmitUtil setSubmitBtn(Button btnSubmit){
this.btnSubmit = btnSubmit;
return this;
}
public SubmitUtil setEditTexts(EditText... editTexts){
this.editTexts = editTexts;
return this;
}
/**
* 同步操作
*/
public SubmitUtil syncOperations(){
for(EditText e:editTexts){
e.addTextChangedListener(this);
}
checkState();
return this;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkState();
}
private void checkState() {
boolean notEmpty = isAllEditTextNotEmpty();
btnSubmit.setEnabled(notEmpty);
}
/**
* 所有EditText輸入內(nèi)容均非空
* @return
*/
private boolean isAllEditTextNotEmpty() {
for(EditText e:editTexts){
if(TextUtils.isEmpty(e.getText().toString().trim())){
return false;
}
}
return true;
}
@Override
public void afterTextChanged(Editable s) {
}
}
首先看下圖讯柔,基本的流程抡蛙。之前寫博客沒畫過流程圖,隨便找了個(gè)軟件畫了下魂迄。流程圖是不是這樣畫的粗截,我不知道,意思大概就是這樣:
這樣思路清晰點(diǎn)极祸,主要工作就是我要寫一個(gè)工具類慈格,可以檢查多個(gè)條件,并且這些條件是可以配置的(比如你要辣椒不要醋遥金,我要醋不要蔥浴捆,他全都要,干擾你哈哈)稿械。當(dāng)一個(gè)條件不滿足時(shí)需要進(jìn)行相應(yīng)的提示选泻,并返回結(jié)果false;當(dāng)所有結(jié)果都滿足時(shí),返回結(jié)果true;思考清楚了美莫,代碼真是呼之欲出页眯。
大概設(shè)計(jì)了下接口如下:
/**
*
* @author XuNeverMore
* @QQ 1045530120
* @create on 2018/2/2 0002
* @github https://github.com/XuNeverMore
*/
public interface IContentChecker {
/**
* 檢查條件
*/
public interface Condition{
/**
* 是否符合條件
* @param body
* @return
*/
boolean match(Body body);
void showTips(Body body);
}
/**
* 被檢查的內(nèi)容
*/
public interface Body{
/**
* 被檢查內(nèi)容的名字
* @return
*/
CharSequence getName();
/**
* 被檢查的具體內(nèi)容
* @return
*/
CharSequence getContent();
}
boolean check(Body body, List<Condition>conditions);
}
OK,下面就是具體的實(shí)現(xiàn)了,為了簡(jiǎn)單易用那肯定要支持鏈?zhǔn)秸{(diào)用厢呵。
ContentBody(檢查內(nèi)容實(shí)現(xiàn)類)
public class ContentBody implements IContentChecker.Body {
private String name;
private String content;
public ContentBody(String name, String content) {
this.name = name;
this.content = content;
}
@Override
public CharSequence getName() {
return name;
}
@Override
public CharSequence getContent() {
return content;
}
}
BaseCondition 具體的條件自己去實(shí)現(xiàn)
public abstract class BaseCondition implements IContentChecker.Condition {
@Override
public void showTips(IContentChecker.Body body) {
}
}
內(nèi)容檢查器窝撵,初步。
ContentChecker
/**
* 內(nèi)容檢查工具類
*
* @author XuNeverMore
* @QQ 1045530120
* @create on 2018/2/2 0002
* @github https://github.com/XuNeverMore
*/
public class ContentChecker implements IContentChecker {
private ContentChecker() {
}
private Body body;
private List<Condition> conditionList;
private ContentChecker(Body body) {
this.body = body;
}
public static ContentChecker getChecker(Body body){
return new ContentChecker(body);
}
/**
* 添加條件
* @param condition
* @return
*/
public ContentChecker addCondition(Condition condition){
if(conditionList == null){
conditionList = new ArrayList<>();
}
conditionList.add(condition);
return this;
}
/**
* 檢查條件并返回檢查結(jié)果
* @return
*/
public boolean getCheckResult(){
if(this.body == null||this.conditionList==null){
throw new NullPointerException("檢查內(nèi)容和條件不能為空");
}
return check(this.body,this.conditionList);
}
@Override
public boolean check(@NonNull Body body, @NonNull List<Condition> conditions) {
for (Condition c : conditions) {
if (!c.match(body)) {
c.showTips(body);
return false;
}
}
return true;
}
}
用法
簡(jiǎn)單的一個(gè)例子判斷手機(jī)號(hào)不能空并且符合手機(jī)號(hào)規(guī)則的襟铭。
ContentBody phoneNumBody = new ContentBody("手機(jī)號(hào)", phoneNum);
boolean checkResult = ContentChecker.getChecker(phoneNumBody)
.addCondition(new BaseCondition() {
@Override
public boolean match(IContentChecker.Body body) {
return RegexUtils.isMobileExact(phoneNum);
}
@Override
public void showTips(IContentChecker.Body body) {
ToastUtils.showShort(body.getName() + "不正確碌奉!");
}
})
.addCondition(new BaseCondition() {
@Override
public boolean match(IContentChecker.Body body) {
return TextUtils.isEmpty(body.getContent());
}
@Override
public void showTips(IContentChecker.Body body) {
ToastUtils.showShort(body.getName()+"手機(jī)號(hào)不能為空");
}
})
.getCheckResult();
自我感覺看起來還不錯(cuò)鏈?zhǔn)秸{(diào)用短曾,結(jié)構(gòu)清晰,比if判斷好些赐劣。每個(gè)條件每次得實(shí)現(xiàn)還是比較繁瑣嫉拐,我們可以把常用的都實(shí)現(xiàn)了,然后用的時(shí)候直接new就行了魁兼。下面是用實(shí)現(xiàn)過了條件類寫的婉徘,更簡(jiǎn)單明了。條件有必要的話可以用單例模式搞出來咐汞。
ContentBody phoneNumBody = new ContentBody("手機(jī)號(hào)", phoneNum);
boolean checkResult = ContentChecker.getChecker(phoneNumBody)
.addCondition(new NonNullCondition())
.addCondition(new PhoneNumCondition())
.getCheckResult();
現(xiàn)在基本上解決了用if檢查的繁瑣了盖呼,還有一個(gè)問題就是檢查多個(gè)的內(nèi)容需要一點(diǎn)點(diǎn)優(yōu)化。所以我寫了一個(gè)內(nèi)部?jī)?nèi)來解決多個(gè)內(nèi)容檢查的問題碉考。
ContentChecker.Machine 用于多項(xiàng)內(nèi)容檢查
public class ContentChecker implements IContentChecker {
private ContentChecker() {
}
private Body body;
private List<Condition> conditionList;
private ContentChecker(Body body) {
this.body = body;
}
public static ContentChecker getChecker(Body body){
return new ContentChecker(body);
}
public ContentChecker addCondition(Condition condition){
if(conditionList == null){
conditionList = new ArrayList<>();
}
conditionList.add(condition);
return this;
}
/**
* 檢查條件并返回檢查結(jié)果
* @return
*/
public boolean getCheckResult(){
if(this.body == null||this.conditionList==null){
throw new NullPointerException("檢查內(nèi)容和條件不能為空");
}
return check(this.body,this.conditionList);
}
@Override
public boolean check(@NonNull Body body, @NonNull List<Condition> conditions) {
for (Condition c : conditions) {
if (!c.match(body)) {
c.showTips(body);
return false;
}
}
return true;
}
public static Machine getCheckMachine(){
return new Machine();
}
public static class Machine{
private List<ContentChecker> checkerList;
private Machine() {
checkerList = new ArrayList<>();
}
public Machine putChecker(ContentChecker checker){
checkerList.add(checker);
return this;
}
/**
* 檢查所有內(nèi)容
* @return 結(jié)果
*/
public boolean checkAll(){
for(ContentChecker c:checkerList){
if(!c.getCheckResult()){
return false;
}
}
return true;
}
}
}
那么現(xiàn)在我檢查手機(jī)號(hào)和密碼就是這樣寫的了塌计。
String phoneNum = mDataBing.edtPhone.getText().toString().trim();
String password = mDataBing.edtPassword.getText().toString().trim();
ContentBody phoneNumBody = new ContentBody("手機(jī)號(hào)", phoneNum);
ContentBody passwordBody = new ContentBody("密碼", password);
boolean result = ContentChecker.getCheckMachine()
.putChecker(ContentChecker.getChecker(phoneNumBody)
.addCondition(new NonNullCondition())
.addCondition(new PhoneNumCondition()))
.putChecker(ContentChecker.getChecker(passwordBody)
.addCondition(new NonNullCondition())
.addCondition(new BaseCondition() {
@Override
public boolean match(IContentChecker.Body body) {
return body.getContent().length() >= 6;
}
@Override
public void showTips(IContentChecker.Body body) {
ToastUtils.showShort("密碼不能少于6位");
}
}))
.checkAll();
result就是檢查手機(jī)號(hào)和密碼最后的結(jié)果。
以下是幾個(gè)常見的檢查條件
/**
* 非空條件
* @author XuNeverMore
* @QQ 1045530120
* @create on 2018/2/2 0002
* @github https://github.com/XuNeverMore
*/
public class NonNullCondition extends BaseCondition{
@Override
public boolean match(IContentChecker.Body body) {
return !TextUtils.isEmpty(body.getContent());
}
@Override
public void showTips(IContentChecker.Body body) {
//因?yàn)檫@里取得是body.getName()所以手機(jī)號(hào)或密碼為空的提示侯谁,該類都適用
ToastUtils.showShort(body.getName()+"不能為空锌仅!");
}
}
/**
* 內(nèi)容長(zhǎng)度檢查條件
* @author XuNeverMore
* @QQ 1045530120
* @create on 2018/2/2 0002
* @github https://github.com/XuNeverMore
*/
public class LengthCondition implements IContentChecker.Condition{
private int length;//最小長(zhǎng)度
public LengthCondition(int length) {
this.length = length;
}
@Override
public boolean match(IContentChecker.Body body) {
return !TextUtils.isEmpty(body.getContent())&&body.getContent().length()>=length;
}
@Override
public void showTips(IContentChecker.Body body) {
ToastUtils.showShort(body.getName()+"長(zhǎng)度不能少于"+length+"位");
}
}