3.27
简单的登录
public class MainActivity extends AppCompatActivity {
private Button signUpButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signUpButton = findViewById(R.id.SignUpButton);
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建一个意图对象,用于启动注册页面的活动
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
// 启动注册页面的活动
startActivity(intent);
}
});
}
/**
* function: 登录
* */
public void login(View view){
EditText EditTextAccount = findViewById(R.id.UserNameEdit);
EditText EditTextPassword = findViewById(R.id.PassWordEdit);
String account = EditTextAccount.getText().toString();
String password = EditTextPassword.getText().toString();
// 检查是否为教师账号,如果是则直接跳转到教师界面
if (account.equals("teacher") && password.equals("teacher")) {
Intent intent = new Intent(MainActivity.this, teacher.class);
startActivity(intent);
} else {
new Thread() {
@Override
public void run() {
StudentDao studentDao = new StudentDao();
int msg = studentDao.login(EditTextAccount.getText().toString(), EditTextPassword.getText().toString());
hand1.sendEmptyMessage(msg);
}
}.start();
}
}
@SuppressLint("HandlerLeak")
final Handler hand1 = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
// 登录成功后启动新的活动
startActivity(new Intent(MainActivity.this, MainActivity2.class));
} else if (msg.what == 0) {
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
} else if (msg.what == 2) {
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
} else if (msg.what == 3) {
Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
}
}
};
}