3、App有登陆页面,提供用户名和密码验证(15分) (1)实现用户名密码验证和跳转功能。给予5分; (2)带有注册功能,可以让用户提前注册并记录(采用轻量级存储功能sharepreferences),下次App激活时自动载入并与输入的用户名和密码进行比对,比对正确后完成跳转。给予10分。
时间: 2024-03-23 18:37:21 浏览: 24
1. 实现用户名密码验证和跳转功能
首先,在布局文件中添加登录页面的UI元素,包括用户名和密码输入框、登录按钮和注册按钮:
```
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
```
在Activity中,我们可以使用SharedPreferences来保存和读取用户的用户名和密码信息。当用户点击登录按钮时,我们可以根据输入的用户名和密码与保存在SharedPreferences中的信息进行比对,如果一致则跳转到主界面,否则提示用户用户名或密码错误。
```
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
String savedPassword = sharedPreferences.getString(username, "");
if (savedPassword.equals(password)) {
// 登录成功,跳转到主界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,提示用户名或密码错误
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
Button btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册页面
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
```
在代码中,我们首先获取用户名和密码输入框的文本内容,然后使用SharedPreferences获取保存的密码信息,如果一致则跳转到主界面,否则提示用户用户名或密码错误。在注册按钮的点击事件中,我们可以跳转到注册页面。
2. 带有注册功能,可以让用户提前注册并记录
在注册页面中,我们可以让用户输入用户名和密码,并将其保存到SharedPreferences中。注册成功后,我们可以跳转回登录页面,并自动填充保存的用户名和密码信息。
```
public class RegisterActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
Button btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(username, password);
editor.apply();
// 注册成功,跳转到登录页面
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putExtra("username", username);
intent.putExtra("password", password);
startActivity(intent);
finish();
}
});
}
}
```
在代码中,我们首先获取用户名和密码输入框的文本内容,然后使用SharedPreferences保存用户名和密码信息。注册成功后,我们可以跳转回登录页面,并使用Intent传递用户名和密码信息,自动填充对应的输入框。
在登录页面中,我们可以在onCreate方法中获取Intent传递的用户名和密码信息,并自动填充对应的输入框:
```
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
if (username != null && password != null) {
etUsername.setText(username);
etPassword.setText(password);
}
// ...
}
```
这样,我们就完成了带有注册功能的用户名密码验证和跳转功能。
相关推荐
![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)
![](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)