菜鸟初尝写博客,排版、内容不好望多多包涵……
这些功能在APP开发都是标配
以下源码都是从我完整项目搬来,可能会不全,如有不对之处请见谅。
新手小花点时间就好。老菜路过呗
原理:
1.输入手机号在本地判断号是否合法
2.发送到服务器成功把返回验证码赋值给一常量,开启个线程倒计时
public class RegisterActivity extends Activity { private Button btn_second; private EditText et_Account, et_verify_code, rPOnce, rPTwice; private ProgressDialog dialog_PD; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register_activity); title = (TextView) findViewById(R.id.title_text); title.setText(getString(R.string.register_title)); btn_second = (Button) findViewById(R.id.btn_second); et_Account = (EditText) findViewById(R.id.et_account); et_verify_code = (EditText) findViewById(R.id.et_verify_code); rPOnce = (EditText) findViewById(R.id.et_pwd); rPTwice = (EditText) findViewById(R.id.et_confirm_pwd); rPOnce.setOnFocusChangeListener(etFocusChangeListener); rPTwice.setOnFocusChangeListener(etFocusChangeListener); } public void doClick(View view) { int id = view.getId(); if (id == R.id.btn_second) { if (TextUtils.isEmpty(et_Account.getText().toString())) { Helper.myToast(this, "手机为空"); } else if (!Helper.isPhoneNumber(et_Account.getText().toString())) { Helper.myToast(this, "手机格式不对"); return; } else { String accountNum = et_Account.getText().toString().trim(); Map<String, String> params = new HashMap<String, String>(); params.put("telephone", accountNum); (new HttpUtilService()).AsynPost(new NetCallbackListener() { @Override public void onSuccess(String result) { Logger.dout("xxxxx"+result); try { JSONObject jsonObject = new JSONObject(result); int code = jsonObject.getInt("code"); if (code==0) { UserInfo.random = jsonObject.getString("random"); Helper.myToast(RegisterActivity.this, "获取成功,请注意查收"); jishi = 120; btn_second.setEnabled(false); btn_second.setTextColor(getResources().getColor( R.color.light_gray)); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(jishi--); } }, 0, 1000); }else if (code == 5) { Helper.myToast(RegisterActivity.this, "该号码已注册"); return; } else if (code==-1) { // UserInfo.random = jsonObject.getString("random"); } } catch (Exception e) { e.printStackTrace(); } } @Override public void onFail(String result) { // TODO Auto-generated method stub dialog_PD.dismiss(); } }, Helper.WEBSITE + "sendMsg", params); } } else if (id == R.id.btn_register) { Logger.dout(UserInfo.random); String once = rPOnce.getText().toString(); String twice = rPTwice.getText().toString(); String verify_code_sms = et_verify_code.getText().toString(); String phone = et_Account.getText().toString(); if (TextUtils.isEmpty(phone) || et_Account.getText().toString().trim().length() == 0) { Helper.myToast(this, "手机为空"); return; } if (TextUtils.isEmpty(et_verify_code.getText().toString()) || et_verify_code.getText().toString().trim().length() == 0) { Helper.myToast(this, "验证码为空"); return; } if (!verify_code_sms.equals(UserInfo.random)) { Helper.myToast(this, "验证码不正确,请重新获取"); return; } if (!once.equals(twice)) { Helper.myToast(this, "密码输入不一致"); return; } if (once.length() < 6) { Helper.myToast(this, "密码长度不能少于6位"); return; } if (!Helper.canNetworkUseful(this)) { return; } dialog_PD = ProgressDialog.show(this, null, "正在加载...请稍后", true, false); dialog_PD.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog_PD.setCancelable(true); Map<String, String> params = new HashMap<String, String>(); params.put("telephone", phone); params.put("password", once); (new HttpUtilService()).AsynPost(new NetCallbackListener() { @Override public void onSuccess(String result) { Logger.dout("xxoo"+result); dialog_PD.dismiss(); try { JSONObject json = new JSONObject(result); int code = json.getInt("code"); Logger.dout("xxoo"+code); if (code == 1) { Helper.myToast(RegisterActivity.this, getString(R.string.register_toast_success)); Intent i = new Intent(RegisterActivity.this,LoginActivity.class); i.putExtra("username", et_Account.getText().toString()); i.putExtra("password", rPOnce.getText().toString()); i.putExtra("finishRegister", true); startActivity(i); RegisterActivity.this.finish(); } else if (code == 2) { Helper.myToast(RegisterActivity.this, "手机已被注册"); } else { Helper.myToast(RegisterActivity.this, "未知错误"); } } catch (Exception e) { e.printStackTrace(); } } @Override public void onFail(String result) { dialog_PD.dismiss(); Helper.myToast(RegisterActivity.this, result); } }, Helper.WEBSITE + "Register", params); } } /** * 焦点切换处理 */ OnFocusChangeListener etFocusChangeListener = new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if (hasFocus) { try { // 设置光标位置:到最后 EditText etTmp = (EditText) v; etTmp.setSelection(etTmp.getText().toString().length()); } catch (Exception e) { Helper.myToast(RegisterActivity.this, "处理输入监听出错"); } } else { // 失去焦点 } } }; private Timer timer;// 计时器 int jishi = 120; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 0) { btn_second.setEnabled(true); btn_second.setText("发送验证码"); btn_second.setTextColor(getResources().getColor( R.color.black)); timer.cancel(); } else { btn_second.setText(msg.what + "秒"); } }; }; /**返回按键*/ public void titleBack(View v) { finish(); } @Override public void onDestroy() { if (timer != null) timer.cancel(); super.onDestroy(); } }XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/beijing" android:orientation="vertical" > <include layout="@layout/title" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="10dp" android:orientation="vertical" > <ImageView android:layout_width="120dip" android:layout_height="140dip" android:layout_gravity="center_horizontal" android:src="@drawable/register_top" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/button_rect_selector" android:gravity="center_vertical" android:orientation="horizontal" android:padding="5dp" android:visibility="gone" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册后才能联系到医生哦~" android:textColor="@color/red" /> </LinearLayout> <LinearLayout style="@style/u_edit_bg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/gray" android:drawablePadding="3dip" android:drawableLeft="@drawable/img_phone" android:textSize="18sp" /> <EditText android:id="@+id/et_account" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:inputType="number" android:maxLength="11" android:hint="请输入11位手机号" android:textColor="@color/black" /> <Button android:id="@+id/btn_second" android:layout_width="80dp" android:layout_height="36dp" android:background="@drawable/bt_register" android:onClick="doClick" android:padding="8dp" android:text="获取验证码" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout style="@style/u_edit_bg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/gray" android:gravity="center" android:drawablePadding="3dip" android:drawableLeft="@drawable/img_code" android:textSize="18sp" /> <EditText android:id="@+id/et_verify_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@null" android:hint="请输入验证码" android:inputType="number" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:id="@+id/ll_password" style="@style/u_edit_bg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:drawablePadding="3dip" android:drawableLeft="@drawable/th_password" android:text="输入密码:" android:textColor="@color/gray" android:textSize="18sp" /> <EditText android:id="@+id/et_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@null" android:hint="6-20位数字或字母" android:inputType="textPassword" android:maxLength="20" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:id="@+id/ll_password1" style="@style/u_edit_bg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认密码:" android:textColor="@color/gray" android:gravity="center" android:drawablePadding="3dip" android:drawableLeft="@drawable/th_password_confirm" android:textSize="18sp" /> <EditText android:id="@+id/et_confirm_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@null" android:hint="再次输入密码" android:inputType="textPassword" android:maxLength="20" android:textColor="@color/black" /> </LinearLayout> <Button android:id="@+id/btn_register" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="23dp" android:layout_marginRight="23dp" android:layout_marginTop="15dp" android:background="@drawable/btn_selector_new" android:onClick="doClick" android:text="注册" android:textColor="@color/white" android:textSize="20dp" /> </LinearLayout> </ScrollView> </LinearLayout>