写一个安卓登录注册的逻辑代码,每次注册的用户名和密码使用数组保存,默认一个用户名和密码
时间: 2023-07-15 09:15:23 浏览: 58
好的,下面是一个简单的安卓登录注册的逻辑代码,使用数组保存用户名和密码信息:
```java
public class LoginActivity extends AppCompatActivity {
// 用户名和密码数组
private String[] usernames = {"admin"};
private String[] passwords = {"password"};
// 登录按钮
private Button loginButton;
// 注册按钮
private Button registerButton;
// 用户名文本框
private EditText usernameEditText;
// 密码文本框
private EditText passwordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// 获取视图组件
loginButton = findViewById(R.id.login_button);
registerButton = findViewById(R.id.register_button);
usernameEditText = findViewById(R.id.username_edittext);
passwordEditText = findViewById(R.id.password_edittext);
// 设置登录按钮点击事件
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
// 验证用户名和密码是否正确
boolean isAuth = false;
for (int i = 0; i < usernames.length; i++) {
if (username.equals(usernames[i]) && password.equals(passwords[i])) {
isAuth = true;
break;
}
}
// 显示结果
if (isAuth) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
// 设置注册按钮点击事件
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
// 将新的用户名和密码添加到数组中
String[] new_usernames = new String[usernames.length + 1];
String[] new_passwords = new String[passwords.length + 1];
System.arraycopy(usernames, 0, new_usernames, 0, usernames.length);
System.arraycopy(passwords, 0, new_passwords, 0, passwords.length);
new_usernames[new_usernames.length - 1] = username;
new_passwords[new_passwords.length - 1] = password;
usernames = new_usernames;
passwords = new_passwords;
// 显示结果
Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}
});
}
}
```
这里只是一个简单的示例,实际开发中需要根据需求进行相应的修改和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)