|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
package com.example.sixaunyi;
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
@ -30,6 +29,7 @@ public class LoginActivity extends AppCompatActivity {
|
|
|
|
|
mLoginButton = findViewById(R.id.login);
|
|
|
|
|
// 设置登录按钮的点击事件
|
|
|
|
|
mLoginButton.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
|
|
attemptLogin();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -37,6 +37,12 @@ public class LoginActivity extends AppCompatActivity {
|
|
|
|
|
String username = mUsername.getText().toString();
|
|
|
|
|
String password = mPassword.getText().toString();
|
|
|
|
|
boolean if_correct = verifyUser(username,password);
|
|
|
|
|
if (!isInputUsernameValid(username)){
|
|
|
|
|
Toast.makeText(this, "用户名非法", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
if (!isInputPasswordValid(password)){
|
|
|
|
|
Toast.makeText(this, "密码非法", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
if (if_correct) {
|
|
|
|
|
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
|
|
|
|
|
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
|
|
|
@ -54,4 +60,37 @@ public class LoginActivity extends AppCompatActivity {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private boolean isInputUsernameValid(String username){
|
|
|
|
|
if (!username.isEmpty()) { // 判断用户名是否为空
|
|
|
|
|
String regex = "^[A-Za-z0-9]+$"; // 合法字符集:大小写字母、数字
|
|
|
|
|
boolean isValid = username.matches(regex); // 判断用户名是否合法
|
|
|
|
|
if (isValid) {
|
|
|
|
|
// 用户名合法,执行操作
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// 用户名包含非法字符,提示用户
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 用户名为空,提示用户
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private boolean isInputPasswordValid(String password){
|
|
|
|
|
if (!password.isEmpty()) { // 判断密码是否为空
|
|
|
|
|
String regex = "^[A-Za-z0-9]+$"; // 合法字符集:大小写字母、数字
|
|
|
|
|
boolean isValid = password.matches(regex); // 判断密码是否合法
|
|
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
|
// 密码合法,执行操作
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// 密码包含非法字符,提示用户
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 用户名为空,提示用户
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|