//判断手机是否有网public boolean isConnected() { // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) try { ConnectivityManager connectivity = (ConnectivityManager) this .getSystemService(this.CONNECTIVITY_SERVICE); if (connectivity != null) { // 获取网络连接管理的对象 NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { // 判断当前网络是否已经连接 if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { } return false;}/** * 隐藏正在加载的进度条 */private Dialog loadDialog;private int dialogNum;public void dismissLoadDialog() { dialogNum--; if (dialogNum > 0) { return; } if (null != loadDialog && loadDialog.isShowing() == true) { loadDialog.dismiss(); loadDialog = null; }}//把数组转化为Drawable对象public static Drawable byteToDrawable(byte[] byteArray) { try { String string = new String(byteArray, "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteArrayInputStream ins = new ByteArrayInputStream(byteArray); return Drawable.createFromStream(ins, null);}/** * 显示正在加载的进度条 */public void showLoadingDialog() { dialogNum++; if (loadDialog != null && loadDialog.isShowing()) { loadDialog.dismiss(); loadDialog = null; } loadDialog = new Dialog(this, R.style.dialog); loadDialog.setCanceledOnTouchOutside(false); loadDialog.setContentView(R.layout.layout_dialog); try { loadDialog.show(); } catch (WindowManager.BadTokenException exception) { exception.printStackTrace(); }}
//检查邮箱private boolean checkEmail() { String emailString = edit_email.getText().toString().trim(); if (TextUtils.isEmpty(emailString)) { hint_emamil.setText("邮箱不能为空"); return false; } Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); Matcher matcher = pattern.matcher(emailString); if (matcher.matches()) { hint_emamil.setText(""); return true; } else { hint_emamil.setText("邮箱格式不正确"); return false; }}private boolean checkPasswork() { String editpasswordsString = edit_password.getText().toString(); if (TextUtils.isEmpty(editpasswordsString)) { hint_passwork.setText("密码不能为空"); return false; } else if (editpasswordsString.length() < 6 || editpasswordsString.length() > 16) { hint_passwork.setText("密码仅限6-16个字符"); return false; } else { hint_passwork.setText(""); return true; }}private boolean checkAgainPasswork() { String editagainpasswordsString = edit_again_password.getText().toString(); if (TextUtils.isEmpty(editagainpasswordsString)) { hint_again_passord.setText("确认密码不能为空"); // shakeViewToNotifyUser(edit_again_password); return false; } else { hint_again_passord.setText(""); String passwordsString = edit_password.getText().toString(); if (!passwordsString.equals(editagainpasswordsString)) { hint_again_passord.setText("密码不一致"); return false; } else { return true; } }}//检查验证码private boolean checkCaptcha() { if (mCaptchaBytes == null) { Toast.makeText(getActivity(), "未获取验证码", Toast.LENGTH_SHORT).show(); return false; } mCaptchaEditTextString = edit_yanzhengma.getText().toString().trim(); if(mCaptchaEditTextString.contains(" ")){ hint_yanzhengma.setText("验证码不正确"); return false; } if (mCaptchaEditTextString == null || "".equals(mCaptchaEditTextString)) { // shakeViewToNotifyUser(edit_yanzhengma); hint_yanzhengma.setText("验证码不能为空"); return false; } else { hint_yanzhengma.setText(""); return true; }} //检查协议private boolean checkXieyi() { CheckBox checkBox = (CheckBox) view.findViewById(R.id.xieyi_check); if (!checkBox.isChecked()) { hint_xieyi.setText("未勾选《央视网网络服务使用协议》"); return false; } else { hint_xieyi.setText(""); return true; }}
使用
if (!isConnected()) { showToast(R.string.network_invalid); return;}//点击注册if (!checkEmail()) { return;}if (!checkPasswork()) { return;}if (!checkAgainPasswork()) { return;}if (!checkCaptcha()) { return;}if (!checkXieyi()) { return;}