输入框手机号校验以及EditText的

// 失去焦点验证
et_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
int index = detail_header_view_list
.indexOf(headerView);
clickHeader(index);
} else {
String phone = headerView.et_phone.getText().toString().trim();

setEditText(et_mobile, phone);
if (!Utils.isEmpty(phone)) {
checkMobile(c.CstGUID, phone,headerView.et_phone);
      }
    }
  }
});

 

/*********************************** 检测手机号**开始 *******************************************/

private void checkMobile(String cstId, String mobile,final EditText et_mobile) {
// 验证格式
if (!Validater.isMobileNumber(mobile)) {
String msg = "请输入正确手机号码!";
et_mobile.setError(msg);//此提示方法挺酷的
showConfirmDialog(msg);
errorMoblieMap.put(mobile, msg);
return;
}

// 判断超度进行截取
if (mobile.length() == 8) {
mobile11 = mobile.substring(0, 8);
} else if (mobile.length() == 10) {
mobile11 = mobile.substring(0, 10);
} else {
mobile11 = mobile.substring(0, 11);
}
if (passedMobileList.contains(mobile11)) {
return;
}

if (errorMoblieMap.containsKey(mobile11)) {
String msg = errorMoblieMap.get(mobile11);

/**
* 字符串截取
*/
String resultList[]=msg.split("。");//以分号截取
String phoneList[]=resultList[2].split(":");//截取手机号
final String phone=phoneList[1].split("O")[0].trim();//手机号
try {
prompt =resultList[0]+" \n"+"接待方式:"+phoneList[2]+" \n";
} catch (Exception e) {
prompt = resultList[0]+" \n";
}

et_mobile.setError(prompt);//设置点击后出现弹出框
showNoticeDialog(msg);
return;
}

final CheckMobileLogic cmLogic = new CheckMobileLogic() {
@Override
public void updateUIBySucess(ResponseBase result) {
dismissDialog();
if (result != null&& result.error.equals(Constants.ERROR_CODE_SUCCESS)) {
passedMobileList.add(mobile11);
if (errorMoblieMap.containsKey(mobile11)) {
errorMoblieMap.remove(mobile11);
}
} else {
dealFailMsgByDialog(ACTION_CHECK, "手机号码", result, false);
if (result != null
&& result.error
.equals(Constants.ERROR_CODE_FOR_SHOW)) {
errorMoblieMap.put(mobile11, result.msg);
et_mobile.setError(result.msg);
}
}
};

@Override
public void updateUIByError(VolleyError error) {
dismissDialog();
dealErrorMsgByDialogNotice(ACTION_CHECK, "手机号码", error);//返回信息错误时,通知上一个人(短信/电话)
if (error != null) {
ResponseBase result = parseError(error);
if (result != null&& result.error.equals(Constants.ERROR_CODE_FOR_SHOW)) {
errorMoblieMap.put(mobile11, result.msg);
/**
* 字符串截取
*/
String resultList[]=result.msg.split("。");//以分号截取
String phoneList[]=resultList[2].split(":");//截取手机号
final String phone=phoneList[1].split("O")[0].trim();//手机号
try {
prompt =resultList[0]+" \n"+"接待方式:"+phoneList[2]+" \n";
} catch (Exception e) {
prompt = resultList[0]+" \n";
}

et_mobile.setError(prompt);//设置点击后出现弹出框
}
}
};
};

showWaitDialog(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cmLogic.cancelRequest();
}
});
cmLogic.checkMobile(oppid, cstId, UserInfo.project.ProjGUID, mobile11);//去检查服务端是否相同
}

 

/**
* 检查客户手机号
* @author JieRain
*/
public class CheckMobileLogic {
private final String TAG_REQUEST="CheckMobile";

/**
* 检查客户手机号
*/
public void checkMobile(String oppId, String cstId, String pid , String mobile) {

NewcgListener listener= new NewcgListener() {
@Override
public void HandlerSuccess(String response) {
ResponseBase result=null;

try {
if(!response.isEmpty())
{
Gson g=new Gson();
result=g.fromJson(response, ResponseBase.class);
}
} catch (Exception e) {
Log.log(TAG_REQUEST, e);
}

updateUIBySucess(result);
}

@Override
public void HandlerError(VolleyError error)
{
updateUIByError(error);
}
};

CustomerRequest.CheckMobile(TAG_REQUEST,oppId, cstId, pid, mobile, listener);
}

public void cancelRequest(){
VersionRequest.cancelRequest(TAG_REQUEST);
}

public void updateUIBySucess(ResponseBase result){};
public void updateUIByError(VolleyError error){};
}

 

/**
* 验证客户手机号是否存在
* @param tag
* @param oppId 销售机会ID
* @param cstId 客户ID
* @param pid 明源系统项目id
* @param tel 手机号
* @param listener
*/
public static void CheckMobile(String tag, String oppId, String cstId, String pid, String tel, NewcgListener listener)
{
String url=String.format(CHECK_CUSTOMER_MOBILE_URL,pid,tel);
if(!Utils.isEmpty(oppId)){
url += "/oppid/" +oppId;
}
if(!Utils.isEmpty(cstId)){
url += "/cstid/" +cstId;
}
url += "/t/"+Utils.getCurrentTime();
NewcgStringRequest request =new NewcgStringRequest(Request.Method.GET,url,null,listener.SuccessListener(),listener.ErrorListener());
request.setTag(tag);
queue.add(request);
}

 

public class NewcgStringRequest extends NewcgRequest <String>{

public NewcgStringRequest(int method, String url, String requestBody, Response.Listener<String> listener,Response.ErrorListener errorListener) {
super(method,url,requestBody,listener,errorListener);
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
Log.log("NewcgStringRequest-Volley-Response", parsed);
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}

/**
*
* <p>Title: NewcgRequest</p>
* <p>Description: 请求基类 </p>
* <p>Company: maxrocky</p>
* @author earl
* @date 2015-9-18
*/
public abstract class NewcgRequest <T> extends Request<T> {

public static final String Client_ID =Constants.Client_ID;
public static String VERSION;
public static final int SOCKET_TIMEOUT=30 * 1000;
/** Charset for request. */
private static final String PROTOCOL_CHARSET = Constants.PROTOCOL_CHARSET;
/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE =String.format("application/json; charset=%s", PROTOCOL_CHARSET);
private final Response.Listener<T> mListener;
private final String mRequestBody;

private Map<String, String> headers;

public NewcgRequest(int method, String url,String requestBody, Response.Listener<T> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
mRequestBody = requestBody;
VERSION = Utils.getVersionName();
headers = new HashMap<String, String>();

CreateHeaders();
setRetryPolicy(new DefaultRetryPolicy(SOCKET_TIMEOUT, 0, 2f));
Log.log("NewcgRequest-Volley-Request", requestBody);
Log.log("NewcgRequest-Volley-URL", url);
}

private void CreateHeaders()//创建请求头
{
headers.put("clientid", Client_ID);
headers.put("version", VERSION);
if(Constants.IS_ZIP){
headers.put("Accept-Encoding", "gzip,deflate");
}
headers.put("accept","application/json");
if(!TextUtils.isEmpty(UserInfo.Access_Token)){
headers.put("access_token",UserInfo.Access_Token);
}
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}

@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}

@Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}

@Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, PROTOCOL_CHARSET);
return null;
}
}

 


/**
* 本地验证手机号码格式 ^1[0-9]{10}(\\-[0-9]){0,1}$
*
* @param mobiles
* @return
*/
public static boolean isMobileNumber(String mobileNumber) {
Pattern p = null;
if (mobileNumber.length() == 8) {
p = Pattern.compile("^(9|6)[0-9]{7}$");//香港和澳门
} else if (mobileNumber.length() == 10) {
p = Pattern.compile("^09[0-9]{8}$");//台湾
} else{
p = Pattern.compile("^1[0-9]{10}(\\-[0-9]){0,1}$");
}
Matcher m = p.matcher(mobileNumber);
return m.matches();
}

posted @ 2015-11-02 14:06  /画家/  阅读(680)  评论(0编辑  收藏  举报