3.18

public class SignUpActivity extends AppCompatActivity {

private EditText id;
private EditText name;
private EditText phone;
private EditText className;
private EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

id = findViewById(R.id.StudentIdEdit);
name = findViewById(R.id.NameEdit);
phone = findViewById(R.id.PhoneEdit);
className = findViewById(R.id.ClassEdit);
password = findViewById(R.id.PasswordEdit);


Button backLoginButton = findViewById(R.id.BackLoginButton);
backLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 返回到 MainActivity
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});

}

public void register(View view){

String id1 = id.getText().toString();
String name1 = name.getText().toString();
String phone1 = phone.getText().toString();
String className1 = className.getText().toString();
String password1 = password.getText().toString();

Student student = new Student();

student.setId(id1);
student.setName(name1);
student.setPhone(phone1);
student.setClassName(className1);
student.setPassword(password1);
student.setSetGoal(0);
student.setSetRecord(0);

new Thread(){
@Override
public void run() {

int msg = 0;
StudentDao studentDao = new StudentDao();

Student s = studentDao.findStudent(student.getId());
if(s != null){
msg = 1;
}
else{
boolean flag = studentDao.register(student);
if(flag){
msg = 2;
}
}
hand.sendEmptyMessage(msg);
}
}.start();
}
@SuppressLint("HandlerLeak")
final Handler hand = new Handler()
{
public void handleMessage(Message msg) {
if(msg.what == 0) {
Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
} else if(msg.what == 1) {
Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();
} else if(msg.what == 2) {
Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
//将想要传递的数据用putExtra封装在intent中
intent.putExtra("a","注册");
setResult(RESULT_CANCELED,intent);
finish();
}
}
};
}
posted @ 2024-03-18 23:37  涨涨涨张  阅读(4)  评论(0编辑  收藏  举报