更新完善关于注册问题 注册采用最基本账户密码模式 重复输入以确认密码正确性
注册时账户有唯一性 会检测是否有重复账户出现 并不能输入非法字符
密码不能输入非法字符 使用可以采用数字英文混杂方式填写 允许大小写
最后选择性别点击确认后即可完成注册
部分代码展示如下:
package com.example.secondhand;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity {
private ImageView backArrow;
private EditText etUserName;
private EditText etPassword;
private EditText et_again;
private Button btnRegister;
private RadioGroup sexRadioGroup;
private String sexStr = "男";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
backArrow = findViewById(R.id.backArrow);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
etUserName = findViewById(R.id.etUserName) ;
etPassword = findViewById(R.id.etPassword) ;
et_again = findViewById(R.id.et_again);
btnRegister = findViewById(R.id.btnRegister) ;
sexRadioGroup = findViewById(R.id.sexRadioGroup);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});
sexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioWoman:
sexStr = "女";
break;
case R.id.radioMan:
sexStr = "男";
break;
}
}
});
}
private String[] arr = new String[3];
public String[] getEditTextContent(){
String username = etUserName.getText().toString().trim();
String password = etPassword.getText().toString().trim();
arr[0] = username;
arr[1] = password;
return arr;
}
private void register() {
String[] editTextContent = getEditTextContent();
String username = editTextContent[0];
String password = etPassword.getText().toString().trim();
String Dermindpassword = et_again.getText().toString().trim();
if (username == null || "".equals(username) || password == null || "".equals(password) || Dermindpassword == null || "".equals(Dermindpassword)) {
//账户或者密码为空
UiUtils.toast("用户名、密码、确认密码均不能为空");
return;
}else if(!Dermindpassword.equals(password)) {
UiUtils.toast("密码和确认密码不相同");
return;
}
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setDermindPassword(Dermindpassword);
user.setSex(sexStr);
long raw = UserDao.getInstance().insertUser(user);
if(raw == -1000){
UiUtils.toast("该用户名已经存在");
}else if(raw == 0){
UiUtils.toast("注册失败");
}else if(raw > 0){
UiUtils.toast("注册成功");
finish();
}
}
}