package com.hjx.personalcenter.fragment; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import com.hjx.personalcenter.R; import com.hjx.personalcenter.activity.RegisterInfoActivity; import com.hjx.personalcenter.http.HttpCode; import com.hjx.personalcenter.http.HttpManager; import com.hjx.personalcenter.util.AlertUtils; import com.hjx.personalcenter.util.PasswordCheckUtils; import com.hjx.personalcenter.util.PhoneNumCheckUtils; import org.json.JSONException; import org.json.JSONObject; /** * 注册 * Created by Administrator on 2016/11/7. */ public class RegisterFragment extends Fragment implements View.OnClickListener { private View mView; private Button btn_register,get_authcode; private EditText phonenumber,password,anth_6num;//;//密码 private ImageView openeyes; private String sourceStr ="android"; private String typeStr = "register"; private int i = 0; private Thread thread; private int type; public static Boolean showPassword = false; public static String TABLAYOUT_FRAGMENT = "tab_fragment"; public static RegisterFragment newInstance(int type) { RegisterFragment fragment = new RegisterFragment(); Bundle bundle = new Bundle(); bundle.putSerializable(TABLAYOUT_FRAGMENT, type); fragment.setArguments(bundle); return fragment; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); try { JSONObject jsonObject; String status; switch (msg.what) { case HttpCode.REGISTERED_SUCESS: jsonObject = (JSONObject) msg.obj; status = jsonObject.getString("status"); //AlertUtils.showToast(RegisterActivity.this, jsonObject.optString("message")); String access_token = jsonObject.getString("access_token"); String userId = jsonObject.getString("userId"); if (status.equals("100")) { String usernameStr = phonenumber.getText().toString().trim(); String passwordStr = password.getText().toString().trim(); //HttpManager.getInstance().saveLoginInfo(usernameStr,passwordStr,access_token,"true",userId); AlertUtils.showToast(getActivity(), "注册成功!"); Intent intent = new Intent(); intent.setClass(getActivity(),RegisterInfoActivity.class); startActivity(intent); getActivity().overridePendingTransition(R.anim.rightin, R.anim.rightout); getActivity().finish(); } break; case HttpCode.REGISTERED_FAIL: AlertUtils.showToast(getActivity(), "注册失败"); break; case HttpCode.AUTHCODE_SUCESS: jsonObject = (JSONObject) msg.obj; status = jsonObject.optString("status"); if (status.equals("100")) { AlertUtils.showToast(getActivity(), "验证码发送成功"); } break; case HttpCode.AUTHCODE_FAIL: jsonObject = (JSONObject) msg.obj; AlertUtils.showToast(getActivity(), "验证码发送失败"); break; case HttpCode.IS_REFISTER: jsonObject = (JSONObject) msg.obj; status = jsonObject.getString("status"); if (!status.equals("true")) { getauthcode(); }else { AlertUtils.showToast(getActivity(), "该手机号已经注册过了"); } break; case 1: get_authcode.setEnabled(false); get_authcode.setClickable(false); get_authcode.setText(Integer.toString(i--)+" s"); if (i<=0){ get_authcode.setEnabled(true); get_authcode.setClickable(true); get_authcode.setText("获取验证码"); } break; } } catch (JSONException e) { e.printStackTrace(); } } }; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (mView == null) { mView = inflater.inflate(R.layout.fragment_loginandregister_register, container, false); initView(mView); initData(); setLister(); } return mView; } private void initView(View mView) { get_authcode = (Button) mView.findViewById(R.id.btn_authcode); anth_6num = (EditText) mView.findViewById(R.id.et_authcode); phonenumber = (EditText) mView.findViewById(R.id.et_phonenumber); password = (EditText) mView.findViewById(R.id.et_password); openeyes= (ImageView) mView.findViewById(R.id.iv_pwd_change); btn_register = (Button) mView.findViewById(R.id.btn_register); } private void initData() { } private void setLister() { get_authcode.setOnClickListener(this); openeyes.setOnClickListener(this); btn_register.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_register: //测试 Intent intent = new Intent(); intent.setClass(getActivity(),RegisterInfoActivity.class); startActivity(intent); getActivity().overridePendingTransition(R.anim.rightin, R.anim.rightout); getActivity().finish(); String usernameStr = phonenumber.getText().toString().trim(); String authcodeStr = anth_6num.getText().toString().trim(); String passwordStr = password.getText().toString().trim(); if (TextUtils.isEmpty(usernameStr) || TextUtils.isEmpty(authcodeStr)||TextUtils.isEmpty(passwordStr)) { AlertUtils.showToast(getActivity(), "手机号,密码和验证码不能为空"); return; } else if(passwordStr.length()<8){ AlertUtils.showToast(getActivity(), "密码不能少于8位"); }else if (!PhoneNumCheckUtils.isPhone(usernameStr)){ AlertUtils.showToast(getActivity(), "手机号码输入错误"); }else if(!passwordStr.matches(PasswordCheckUtils.PASSWORD_CHAR_AND_NUMBER)){ AlertUtils.showToast(getActivity(), "密码由英文和数字组成"); }else { register(); } break; case R.id.iv_pwd_change: if(showPassword){//显示密码 showPassword = !showPassword; openeyes.setImageResource(R.mipmap.pwd_open); password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); password.setSelection(password.getText().toString().length()); }else{//隐藏密码 showPassword = !showPassword; openeyes.setImageResource(R.mipmap.pwd_hide); password.setTransformationMethod(PasswordTransformationMethod.getInstance()); password.setSelection(password.getText().toString().length()); } break; case R.id.btn_authcode: usernameStr = phonenumber.getText().toString().trim(); if (!PhoneNumCheckUtils.isPhone(usernameStr)){ AlertUtils.showToast(getActivity(), "请输入正确的手机号"); }else { anth_6num.requestFocus(); ifregister(); } break; } } //获取验证码 private void getauthcode() { String usernameStr = phonenumber.getText().toString().trim(); HttpManager.getInstance().authCode(typeStr, usernameStr, handler, getActivity()); i = 60; if(thread == null){ thread = new Thread( new ThreadShow()); thread.start(); } } private void ifregister() { String usernameStr = phonenumber.getText().toString().trim(); HttpManager.getInstance().isregistered(usernameStr, handler, getActivity()); } private void register() { String usernameStr = phonenumber.getText().toString().trim(); String authcodeStr = anth_6num.getText().toString().trim(); String passwordStr = password.getText().toString().trim(); HttpManager.getInstance().register(getActivity(), usernameStr, passwordStr, authcodeStr, sourceStr,handler); } // 线程类 定时器 class ThreadShow implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(1000); Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); System.out.println("send..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("thread error..."); } } } } }