账号注册之手机号注册

  

activity_register.xml

<LinearLayout
.
.
.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/height_12_160"
android:background="@color/colorPrimary">

<ImageButton
android:id="@+id/common_back_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/transparent"
android:paddingLeft="@dimen/width_5_160"
android:paddingRight="@dimen/width_5_160"
android:src="@drawable/common_back_normal"/>

<TextView
android:id="@+id/common_back_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="@dimen/width_5_160"
android:paddingRight="@dimen/width_5_160"
android:text="登陆"
android:textColor="@color/white"
android:visibility="gone"
/>

<TextView
android:id="@+id/regsiter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="注册"
android:textColor="@color/white"
android:textSize="18sp"/>
</RelativeLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/width_18_160"
android:orientation="vertical">

<com.rey.material.widget.EditText
android:id="@+id/reg_phone_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/height_20_160"
android:hint="手机号"
android:imeOptions="actionNext"
android:inputType="phone"
android:textSize="14sp"
app:et_dividerColor="@color/weixin_bgcolor"
app:et_dividerErrorColor="@color/red"
app:et_dividerHeight="@dimen/width_1_160"
app:et_dividerPadding="@dimen/height_1_160"
app:et_labelEnable="true"
app:et_supportMode ="charCounter"

app:et_labelTextSize="10sp"
app:et_supportMaxChars="11"
app:et_supportTextColor="@color/weixin_bgcolor"
app:et_supportTextErrorColor="@color/red"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="@dimen/height_2_160"
android:layout_marginBottom="@dimen/height_2_160"

android:orientation="horizontal">

<com.rey.material.widget.EditText
android:id="@+id/id_code_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="验证码"
android:imeOptions="actionNext"
android:inputType="number"
android:textSize="14sp"
app:et_dividerColor="@color/weixin_bgcolor"
app:et_dividerErrorColor="@color/red"
app:et_dividerHeight="@dimen/width_1_160"
app:et_dividerPadding="@dimen/height_1_160"
app:et_labelEnable="true"
app:et_labelTextSize="10sp"
app:et_supportMaxChars="6"
app:et_supportMode ="charCounter"
app:et_supportTextColor="@color/weixin_bgcolor"
app:et_supportTextErrorColor="@color/red"
/>

<com.rey.material.widget.Button
android:id="@+id/id_send_btn"
style="@style/Material.Drawable.Ripple.Wave.Light"
android:layout_width="@dimen/width_40_160"
android:layout_height="@dimen/height_7_160"
android:layout_marginLeft="@dimen/width_3_160"
android:background="@drawable/common_btn_bg"
android:paddingLeft="@dimen/width_3_160"
android:paddingRight="@dimen/width_3_160"
android:text="获取验证码"
android:textColor="@color/white"
android:textSize="12sp"
app:rd_enable="true"/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<com.rey.material.widget.EditText
android:id="@+id/id_password_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="密码"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:textSize="14sp"
app:et_dividerColor="@color/weixin_bgcolor"
app:et_dividerErrorColor="@color/red"
app:et_dividerHeight="@dimen/width_1_160"
app:et_dividerPadding="@dimen/height_1_160"
app:et_labelEnable="true"
app:et_labelTextSize="10sp"
app:et_supportMode ="charCounter"

app:et_supportTextColor="@color/weixin_bgcolor"
app:et_supportTextErrorColor="@color/red"
/>

<LinearLayout
android:layout_marginLeft="@dimen/width_3_160"

android:layout_width="@dimen/width_40_160"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">


<com.rey.material.widget.CheckBox
android:id="@+id/chat_register_password_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Material.Drawable.CheckBox"
app:cbd_strokeColor="@color/setting_switch_color_selector" />
<TextView
android:id="@+id/id_addnote_permission_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/width_1_160"
android:text="显示密码"
android:textColor="@color/white"
android:textSize="12sp"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

<LinearLayout
android:id="@+id/register_protocol_checkbox_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/height_1_160"
android:layout_marginLeft="@dimen/width_18_160"
android:layout_marginTop="@dimen/height_2_160"
android:gravity="center_vertical"
android:orientation="horizontal"
>

<com.rey.material.widget.CheckBox
android:id="@+id/register_protocol_checkbox"
style="@style/Material.Drawable.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
app:cbd_strokeColor="@color/setting_switch_color_selector"
/>


<TextView
android:id="@+id/register_protocol_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/width_1_160"
android:text="已同意用户协议"
android:textColor="@color/colorPrimary"
android:textSize="12sp"/>
</LinearLayout>

<com.rey.material.widget.Button
android:id="@+id/id_reg_btn"
style="@style/Material.Drawable.Ripple.Wave.Light"
android:layout_width="match_parent"
android:layout_height="@dimen/height_10_160"
android:layout_marginLeft="@dimen/width_18_160"
android:layout_marginRight="@dimen/width_18_160"
android:background="@drawable/common_btn_bg"
android:text="注 册"
android:textColor="@color/white"
android:textSize="14sp"
app:rd_enable="true"
/>

<com.rey.material.widget.Button
android:id="@+id/id_reset_btn"
style="@style/Material.Drawable.Ripple.Wave.Light"
android:layout_width="match_parent"
android:layout_height="@dimen/height_10_160"
android:layout_marginLeft="@dimen/width_18_160"
android:layout_marginRight="@dimen/width_18_160"
android:background="@drawable/common_btn_bg"
android:enabled="false"
android:text="重 置"
android:textColor="@color/white"
android:textSize="14sp"
android:visibility="gone"
app:rd_enable="true"
/>

</LinearLayout>


RegisterActivity.java
public class RegisterActivity extends BaseActivity {

EditText mPhoneET;
EditText mCodeET;
EditText mPasswordET;
Button mResetBtn;
Button mSendBtn;
Button mRegBtn;
ImageButton mBackBtn;
TextView mBackTV;
TextView mTitleTV;
TextView mProtocolTV;
LinearLayout mProtocolLayout;
CheckBox mPasswordCB;
CheckBox mProtocolCB;

private TimeCount time;
private String areaStr = null;
private boolean isForgetPass = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
LogUtil.e(TAG, "onCreate");
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case Constants.Tags.LOAD_DATA_SUCCESS:
final String phone = mPhoneET.getText().toString().trim();
final String password = mPasswordET.getText().toString().trim();
final String code = mCodeET.getText().toString().trim();
UserBean userBean = new UserBean();
userBean.setPassword(password);
userBean.setUsername(phone);
userBean.setCode(Integer.parseInt(code));
userBean.setArea(areaStr);
userBean.setAvatar("http://file.bmob.cn/M02/79/0B/oYYBAFawXZOALjy5AAACPGs5RvU833.png");
//从后端云获取所需信息
userBean.setBgurl("http://file.bmob.cn/M02/79/0B/oYYBAFawXXqAAX_OAAhPzr3vBNo866.png");
userBean.setGender(1);
userBean.setNickname("");
userBean.setRid(0);
userBean.setSignature("开启一段美好旅程吧!");
userBean.signUp(new SaveListener<UserBean>() {
@Override
public void done(UserBean userBean, cn.bmob.v3.exception.BmobException e) {
if (e == null) {
LogUtil.i("TAG", "reg success");
Toast.makeText(RegisterActivity.this, "注册成功,请登录", Toast.LENGTH_SHORT).show();
finish();
} else {
if (e.getErrorCode() == 202) {
CommonUtils.showToast(RegisterActivity.this, "该用户已注册");
} else {
CommonUtils.showToast(RegisterActivity.this, "注册失败");
}
LogUtil.i(TAG, "reg error=" + e.getMessage());
}
}
});
break;
}
}
};
mPhoneET = (EditText) findViewById(R.id.reg_phone_et);
mCodeET = (EditText) findViewById(R.id.id_code_et);
mPasswordET = (EditText) findViewById(R.id.id_password_et);
mResetBtn = (Button) findViewById(R.id.id_reset_btn);
mSendBtn = (Button) findViewById(R.id.id_send_btn);
mRegBtn = (Button) findViewById(R.id.id_reg_btn);
mBackBtn = (ImageButton) findViewById(R.id.common_back_btn);
mBackTV = (TextView) findViewById(R.id.common_back_tv);
mTitleTV = (TextView) findViewById(R.id.regsiter_title);
mProtocolTV = (TextView) findViewById(R.id.register_protocol_tv);
mProtocolLayout = (LinearLayout) findViewById(R.id.register_protocol_checkbox_layout);
mPasswordCB = (CheckBox) findViewById(R.id.chat_register_password_checkbox);
mProtocolCB = (CheckBox) findViewById(R.id.register_protocol_checkbox);

isForgetPass = this.getIntent().getBooleanExtra("resetpass", false);//从引导页过来

mBackBtn.setVisibility(View.VISIBLE);
mBackTV.setVisibility(View.GONE);

if (isForgetPass) {
mProtocolLayout.setVisibility(View.GONE);
mRegBtn.setVisibility(View.GONE);
mResetBtn.setVisibility(View.VISIBLE);
mTitleTV.setText("找回密码");
} else {
mRegBtn.setVisibility(View.VISIBLE);
mResetBtn.setVisibility(View.GONE);
mTitleTV.setText("注册");
}
mBackTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
finish();
}
});
mBackBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mProtocolLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mProtocolCB.isChecked()) {
mProtocolCB.setChecked(false);
mProtocolTV.setTextColor(ContextCompat.getColor(RegisterActivity.this, R.color.grey));
} else {
mProtocolCB.setChecked(true);
mProtocolTV.setTextColor(ContextCompat.getColor(RegisterActivity.this, R.color.colorPrimary));
}
}
});
time = new TimeCount(60000, 1000);// 构造CountDownr对象

mSendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String mPhoneStr = mPhoneET.getText().toString().trim()
.replaceAll(" ", "");
if (StringUtils.isBlank(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号不能为空");
} else if (!StringUtils.isMobileNumber(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号输入有误");
} else {
// 先查询手机号是否已注册
BmobQuery<UserBean> bmobQuery = new BmobQuery<UserBean>();
//查询mobile叫mPhoneStr的数据
bmobQuery.addWhereEqualTo("username", mPhoneStr);
bmobQuery.findObjects(new FindListener<UserBean>() {
@Override
public void done(List<UserBean> list, cn.bmob.v3.exception.BmobException e) {
if (e == null) {
if (list.size() > 0) {
Toast.makeText(RegisterActivity.this, "该手机号已注册,请直接登录", Toast.LENGTH_SHORT).show();
mCodeET.setText("");
mPasswordET.setText("");
} else {
mSendBtn.setText("获取中...");
BmobSMS.requestSMSCode(mPhoneET.getText().toString().trim().replaceAll(" ", ""), getResources().getString(R.string.app_name), new QueryListener<Integer>() {
@Override
public void done(Integer smsId, BmobException ex) {
if (ex == null) {//验证码发送成功
LogUtil.i("bmob", "短信id:" + smsId);//用于查询本次短信发送详情
time.start();// 开始倒计时
CommonUtils.showToast(RegisterActivity.this, "验证码已经发送");
mPhoneET.setTextColor(getResources().getColor(R.color.grey));
mPhoneET.setEnabled(false);
}
}
});

}

} else {
Toast.makeText(RegisterActivity.this, "注册失败,请稍后重试"+e.getMessage(), Toast.LENGTH_SHORT).show();

}
}
});


}

}
});

mPasswordCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mPasswordCB.setChecked(true);
//动态设置密码是否可见
mPasswordET
.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
mPasswordCB.setChecked(false);
mPasswordET
.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
mPasswordET.setSelection(mPasswordET.getText().toString().length());//设置光标位置在文本框末尾
}
});


mRegBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i("TAG", "reging");
final String mPhoneStr = mPhoneET.getText().toString().trim()
.replaceAll(" ", "");
final String mPasswordStr = mPasswordET.getText().toString().trim();
final String mCodeStr = mCodeET.getText().toString().trim();

if (StringUtils.isBlank(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号不能为空");
} else if (!StringUtils.isMobileNumber(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号输入格式有误");
} else if (StringUtils.isEmpty(mPasswordStr)) {
CommonUtils.showToast(RegisterActivity.this, "密码不能为空");
} else if (StringUtils.isEmpty(mCodeStr)) {
CommonUtils.showToast(RegisterActivity.this, "验证码不能为空");
} else if (!mProtocolCB.isChecked()) {
CommonUtils.showToast(RegisterActivity.this, "您还未同意用户协议");
} else {
CommonUtils.showProgressDialog(RegisterActivity.this, "正在注册");
// 先查询手机号是否已注册
BmobQuery<UserBean> bmobQuery = new BmobQuery<UserBean>();
//查询mobile叫mPhoneStr的数据
bmobQuery.addWhereEqualTo("username", mPhoneStr);
bmobQuery.findObjects(new FindListener<UserBean>() {
@Override
public void done(List<UserBean> list, cn.bmob.v3.exception.BmobException e) {
if (e == null) {
if (list.size() > 0) {
CommonUtils.hideProgressDialog();
Toast.makeText(RegisterActivity.this, "该手机号已注册,请直接登录", Toast.LENGTH_SHORT).show();
mCodeET.setText("");
mPasswordET.setText("");
} else {
BmobSMS.verifySmsCode(mPhoneET.getText()
.toString().replaceAll(" ", ""), mCodeStr, new UpdateListener() {
@Override
public void done(BmobException ex) {
CommonUtils.hideProgressDialog();
if (ex == null) {//短信验证码已验证成功
LogUtil.i("bmob", "验证通过");
onRegister();
} else {
CommonUtils.showToast(RegisterActivity.this, "验证码验证失败"+ ",msg = " + ex.getLocalizedMessage());
LogUtil.i("bmob", "验证失败:code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
}
}
});
}

} else {
CommonUtils.hideProgressDialog();
Toast.makeText(RegisterActivity.this, "注册失败,请稍后重试"+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});

}
}
});

mResetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i("TAG", "reset");
final String mPhoneStr = mPhoneET.getText().toString().trim()
.replaceAll(" ", "");
final String mPasswordStr = mPasswordET.getText().toString().trim();
final String mCodeStr = mCodeET.getText().toString().trim();
if (StringUtils.isBlank(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号不能为空");
} else if (!StringUtils.isMobileNumber(mPhoneStr)) {
CommonUtils.showToast(RegisterActivity.this, "手机号输入格式有误");
} else if (StringUtils.isEmpty(mPasswordStr)) {
CommonUtils.showToast(RegisterActivity.this, "密码不能为空");
} else if (StringUtils.isEmpty(mCodeStr)) {
CommonUtils.showToast(RegisterActivity.this, "验证码不能为空");
} else {
// 先查询手机号是否已注册
BmobQuery<UserBean> bmobQuery = new BmobQuery<UserBean>();
//查询mobile叫mPhoneStr的数据
bmobQuery.addWhereEqualTo("username", mPhoneStr);
bmobQuery.findObjects(new FindListener<UserBean>() {
@Override
public void done(List<UserBean> list, cn.bmob.v3.exception.BmobException e) {
if (e == null) {
if (list.size() == 1) {
String mPasswordStr = mPasswordET.getText().toString().trim();
UserBean userBean = new UserBean();
userBean.setPassword(mPasswordStr);
userBean.update(list.get(0).getObjectId(), new UpdateListener() {
@Override
public void done(cn.bmob.v3.exception.BmobException e) {
if (e == null) {
Toast.makeText(RegisterActivity.this, "重置密码成功", Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(RegisterActivity.this, "更新失败,请稍后重试"+ e.getMessage(), Toast.LENGTH_SHORT).show();

}
}
});

} else {
Toast.makeText(RegisterActivity.this, "更新失败,请稍后重试", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "注册失败,请稍后重试" + e.getMessage(), Toast.LENGTH_SHORT).show();

}
}
});

}
}
});
}

@Override
public void onBackPressed() {
finish();

}

private void onRegister() {
final String phone = mPhoneET.getText().toString().trim()
.replaceAll(" ", "");
// 获取手机号码归属地作为地区值
new Thread(new Runnable() {
@Override
public void run() {
// areaStr = retrofitWithGet(phone);
areaStr = "北京";
LogUtil.i(TAG,"getMobileArea" + areaStr);
mHandler.sendEmptyMessage(Constants.Tags.LOAD_DATA_SUCCESS);
}
}).start();


}

/* 定义一个倒计时的内部类 */
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
}

@Override
public void onFinish() {// 计时完毕时触发
mSendBtn.setClickable(true);
mSendBtn.setText("重新获取");
}

@Override
public void onTick(long millisUntilFinished) {// 计时过程显示
mSendBtn.setClickable(false);
mSendBtn.setText(millisUntilFinished / 1000 + "s");
}
}
}

UserBean.java

public class UserBean extends BmobUser {

/**
* 验证码,只作记录用
*/
Integer code;
public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}
}

BaseActivity.java

public abstract class BaseActivity extends Activity {

protected BaseApplication mApplication;
protected Handler mHandler;
protected String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppManager.getInstance().addActivity(this);
NetWorkUtils.networkStateTips(this);
mApplication = (BaseApplication) getApplication();
TAG=this.getLocalClassName();
}

}



猜你喜欢

转载自www.cnblogs.com/yunting/p/9022876.html