话不多说,今天介绍的是用户注册功能模块。
以下图片是我项目中的注册界面:
接下来进入正题——————》》》》》
package com.CWT.navigation.Personal_center;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.CWT.navigation.R;
public class Register extends AppCompatActivity {
//用户名,密码,再次输入的密码的控件
private EditText et_user_name,et_psw,et_psw_again,et_email,et_phonenumber,et_signature;
//用户名,密码,再次输入的密码的控件的获取值
private String userName,psw,pswAgain,email,phonenumber,Ssex,signature;
private RadioGroup Sex;
private Button btn_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置页面布局 ,注册界面
setContentView(R.layout.register);
//设置此界面为竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
init();
}
private void init() {
//从activity_register.xml 页面中获取对应的UI控件
btn_register = (Button) findViewById(R.id.btn_register);
et_user_name= (EditText) findViewById(R.id.et_user_name);
et_psw= (EditText) findViewById(R.id.et_psw);
et_psw_again= (EditText) findViewById(R.id.et_psw_again);
Sex= (RadioGroup) findViewById(R.id.SexRadio);
et_email= (EditText) findViewById(R.id.email);
et_phonenumber= (EditText) findViewById(R.id.phonenumber);
et_signature=(EditText) findViewById(R.id.sign);
//注册按钮
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入在相应控件中的字符串
getEditString();
//判断输入框内容
int sex;
int sexChoseId = Sex.getCheckedRadioButtonId();
switch (sexChoseId) {
case R.id.mainRegisterRdBtnFemale:
sex = 0;
Ssex="女";
break;
case R.id.mainRegisterRdBtnMale:
sex = 1;
Ssex="男";
break;
default:
sex = -1;
break;
}
if(TextUtils.isEmpty(userName)){
Toast.makeText(Register.this, "请输入用户名", Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(psw)){
Toast.makeText(Register.this, "请输入密码", Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(pswAgain)) {
Toast.makeText(Register.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(phonenumber)) {
Toast.makeText(Register.this, "请输入手机号", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(email)) {
Toast.makeText(Register.this, "请输入邮箱账号", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(signature)) {
Toast.makeText(Register.this, "请输入个性签名", Toast.LENGTH_SHORT).show();
}else if (sex<0){
Toast.makeText(Register.this, "请选择性别", Toast.LENGTH_SHORT).show();
}else if(!psw.equals(pswAgain)){
Toast.makeText(Register.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
/**
*从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
*/
}else if(isExistUserName(userName)){
Toast.makeText(Register.this, "此账户名已经存在", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
//把账号、密码和账号标识保存到sp里面
/**
* 保存账号和密码到SharedPreferences中
*/
saveRegisterInfo(userName, psw,phonenumber,email,Ssex,signature);
//注册成功后把账号传递到LoginActivity.java中
// 返回值到loginActivity显示
Intent data = new Intent();
data.putExtra("userName", userName);
setResult(RESULT_OK, data);
//RESULT_OK为Activity系统常量,状态码为-1,
// 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
Register.this.finish();
}
}
});
}
/**
* 获取控件中的字符串
*/
private void getEditString(){
userName=et_user_name.getText().toString().trim();
psw=et_psw.getText().toString().trim();
pswAgain=et_psw_again.getText().toString().trim();
phonenumber=et_phonenumber.getText().toString().trim();
email=et_email.getText().toString().trim();
signature=et_signature.getText().toString().trim();
}
/**
* 从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
*/
private boolean isExistUserName(String userName){
boolean has_userName=false;
//mode_private SharedPreferences sp = getSharedPreferences( );
// "loginInfo", MODE_PRIVATE
SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
//获取密码
String spPsw=sp.getString(userName, "");//传入用户名获取密码
//如果密码不为空则确实保存过这个用户名
if(!TextUtils.isEmpty(spPsw)) {
has_userName=true;
}
return has_userName;
}
/**
* 保存数据到SharedPreferences中SharedPreferences
*/
private void saveRegisterInfo(String userName, String psw, String phonenumber ,
String email,String sex,String signature){
String md5Psw = MD5Utils.md5(psw);//把密码用MD5加密
//loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( );
SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
//获取编辑器, SharedPreferences.Editor editor -> sp.edit();
SharedPreferences.Editor editor=sp.edit();
editor.putString("phonenumber", phonenumber);
editor.putString("sex", sex);
editor.putString("email", email);
editor.putString("userName", userName);
editor.putString("signature", signature);
editor.putString(userName, md5Psw);
editor.apply();
}
}
代码实现中,基本上都有代码注解,我就不再解释了。
请各位大神指教,本博文内容可能写的有错误,有读者发现错误,可以留言我,希望本博文能够帮到大家