1,定义注解类
import android.support.annotation.StringDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@StringDef({SmsType.BIND, SmsType.GENERAL, SmsType.REG, SmsType.EDITPWD, SmsType.LOGIN})
@Retention(RetentionPolicy.SOURCE)
public @interface SmsType {
String BIND = "bind";
String GENERAL = "general";
String REG = "reg";
String EDITPWD = "editpwd";
String LOGIN = "login";
}
2,在形参中添加注解约束@SmsType String type
import android.content.Context;
import com.jiyou.sdklibrary.base.BasePresenter;
import com.jiyou.sdklibrary.mvp.cons.SmsType;
import com.jiyou.sdklibrary.mvp.view.VerificationCodeView;
public interface VerificationCodePresenter extends BasePresenter<VerificationCodeView> {
void getCode(String phone, @SmsType String type, Context context);
}
3,在实现中,形参不需要添加注解约束@SmsType String type
@Override
public void getCode(String phone, String type, Context context) {
boolean phoneTag = phone.length() == 11;
if ((TextUtils.isEmpty(phone))) {
verificationCodeView.showAppInfo("", "手机号输入为空");
return;
} else {
if (phoneTag) {
getSmsCodeMethod(context, HttpUrlConstants.URL_SDK_SMSCODE, phone, type);
} else {
verificationCodeView.showAppInfo("", "手机号长度格式错误");
return;
}
}
}
4,在调用改方法时,参数被约束了,成功。
verificationCodePresenterImp.getCode(mPhone, SmsType.BIND, JYSdkBindActivity.this);
具体分析可看Android 注解约束参数
网友评论