Android写一个登录页面和注册页面,可以让用户提前注册并记录(采用轻量级存储功能sharepreferences),下次App激活时自动载入并与输入的用户名和密码进行比对,比对正确后完成跳转
时间: 2024-03-27 10:40:13 浏览: 26
好的,针对您的问题,我可以提供一个简单的示例代码,帮助您实现Android登录页面和注册页面,并使用SharedPreferences来存储和读取用户信息。
在示例代码中,我们假设登录页面和注册页面分别为LoginActivity和RegisterActivity,用户需要在注册页面输入用户名和密码,然后在登录页面输入相同的用户名和密码即可成功登录。
首先,我们需要在RegisterActivity中实现用户注册,代码如下:
```java
public class RegisterActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mUsernameEditText = findViewById(R.id.username_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
// 获取SharedPreferences的编辑器
SharedPreferences.Editor editor = sp.edit();
// 存储用户信息
editor.putString("username", mUsernameEditText.getText().toString());
editor.putString("password", mPasswordEditText.getText().toString());
// 提交修改
editor.apply();
// 注册成功,跳转到登录页面
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
在上述代码中,我们通过findViewById()方法获取了用户名和密码的EditText控件,并通过SharedPreferences来存储用户信息。当用户点击注册按钮后,我们将用户名和密码存储到SharedPreferences中,并通过Intent跳转到登录页面。
接下来,我们需要在LoginActivity中实现用户登录,代码如下:
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameEditText = findViewById(R.id.username_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
// 获取存储的用户名和密码
String username = sp.getString("username", "");
String password = sp.getString("password", "");
// 比对用户名和密码
if (mUsernameEditText.getText().toString().equals(username)
&& mPasswordEditText.getText().toString().equals(password)) {
// 登录成功,跳转到主页
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,弹出提示信息
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
在上述代码中,我们同样通过findViewById()方法获取了用户名和密码的EditText控件,并通过SharedPreferences来读取之前存储的用户信息。当用户点击登录按钮后,我们将输入的用户名和密码与SharedPreferences中的信息进行比对,如果比对成功,则跳转到主页,否则弹出提示信息。
最后,我们需要在AndroidManifest.xml文件中设置应用程序的启动页面为LoginActivity,代码如下:
```xml
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
```
这样,当应用程序启动时,就会自动跳转到登录页面,并在用户登录成功后跳转到主页。
希望这个示例代码能够帮到您,如有任何问题或者需要进一步的帮助,请随时提出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)