private Button login;// 登录按钮 private CheckBox rememberPsdBox, autoLoginBox;// 记住密码、自动登陆复选框 private EditText name, psd;// 用户名和密码 private TextView userInfo; SharedPreferences loginPreferences, accessPreferences;// 保存登陆信息和访问次数 SharedPreferences.Editor loginEditor, accessEditor;// 对应的编辑器 String userName; String userPsd; boolean isSavePsd,isAutoLogin; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loginPreferences = this.getSharedPreferences("login", Context.MODE_PRIVATE); //补充完整-1:采用参数"access"和Context.MODE_PRIVATE,实例化accessPreferences对象 int count=accessPreferences.getInt("count",1); Toast.makeText(MainActivity.this,"欢迎您,这是第"+count+"次访问!", Toast.LENGTH_LONG).show(); loginEditor = loginPreferences.edit(); //补充完整-2:从accessPreferences中,实例化accessEditor编辑器 accessEditor.putInt("count",++count); accessEditor.commit(); //提交 userName = loginPreferences.getString("name", null); userPsd = loginPreferences.getString("psd", null); isSavePsd=loginPreferences.getBoolean("isSavePsd",false); isAutoLogin=loginPreferences.getBoolean("isAutoLogin", false); if (isAutoLogin) { this.setContentView(R.layout.activity_welcome); userInfo = (TextView) findViewById(R.id.userInfo); userInfo.setText("欢迎您:" + userName + ",登陆成功!"); } else{ loadActivity(); } } public void loadActivity() { this.setContentView(R.layout.activity_main); login = (Button) findViewById(R.id.login); rememberPsdBox = (CheckBox) findViewById(R.id.rememberPsd); autoLoginBox = (CheckBox) findViewById(R.id.autoLogin); name = (EditText) findViewById(R.id.name); psd = (EditText) findViewById(R.id.psd); if (isSavePsd) { psd.setText(userPsd); name.setText(userName); rememberPsdBox.setChecked(true); } login.setOnClickListener(new OnClickListener() { public void onClick(View v) { loginEditor.putString("name", name.getText().toString()); loginEditor.putString("psd", psd.getText().toString()); loginEditor.putBoolean("isSavePsd", rememberPsdBox.isChecked()); loginEditor.putBoolean("isAutoLogin", autoLoginBox.isChecked()); //补充完整-3:loginEditor的commit方法提交数据 MainActivity.this.setContentView(R.layout.activity_welcome); userInfo = (TextView) findViewById(R.id.userInfo); userInfo.setText("欢迎您:" + name.getText().toString() + ",登陆成功!"); } }); }补充代码
时间: 2024-03-12 10:48:25 浏览: 28
根据代码,需要补充完整-1、完整-2和完整-3处的代码。
完整-1:
```java
accessPreferences = this.getSharedPreferences("access", Context.MODE_PRIVATE);
accessEditor = accessPreferences.edit();
```
完整-2:
```java
accessEditor = accessPreferences.edit();
```
完整-3:
```java
loginEditor.commit();
```
补充完整后的代码如下:
```java
private Button login;// 登录按钮
private CheckBox rememberPsdBox, autoLoginBox;// 记住密码、自动登陆复选框
private EditText name, psd;// 用户名和密码
private TextView userInfo;
SharedPreferences loginPreferences, accessPreferences;// 保存登陆信息和访问次数
SharedPreferences.Editor loginEditor, accessEditor;// 对应的编辑器
String userName;
String userPsd;
boolean isSavePsd, isAutoLogin;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginPreferences = this.getSharedPreferences("login", Context.MODE_PRIVATE);
accessPreferences = this.getSharedPreferences("access", Context.MODE_PRIVATE); // 补充完整-1:采用参数"access"和Context.MODE_PRIVATE,实例化accessPreferences对象
int count = accessPreferences.getInt("count", 1);
Toast.makeText(MainActivity.this, "欢迎您,这是第" + count + "次访问!", Toast.LENGTH_LONG).show();
loginEditor = loginPreferences.edit();
accessEditor = accessPreferences.edit(); // 补充完整-2:从accessPreferences中,实例化accessEditor编辑器
userName = loginPreferences.getString("name", null);
userPsd = loginPreferences.getString("psd", null);
isSavePsd = loginPreferences.getBoolean("isSavePsd", false);
isAutoLogin = loginPreferences.getBoolean("isAutoLogin", false);
if (isAutoLogin) {
this.setContentView(R.layout.activity_welcome);
userInfo = (TextView) findViewById(R.id.userInfo);
userInfo.setText("欢迎您:" + userName + ",登陆成功!");
} else {
loadActivity();
}
}
public void loadActivity() {
this.setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
rememberPsdBox = (CheckBox) findViewById(R.id.rememberPsd);
autoLoginBox = (CheckBox) findViewById(R.id.autoLogin);
name = (EditText) findViewById(R.id.name);
psd = (EditText) findViewById(R.id.psd);
if (isSavePsd) {
psd.setText(userPsd);
name.setText(userName);
rememberPsdBox.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginEditor.putString("name", name.getText().toString());
loginEditor.putString("psd", psd.getText().toString());
loginEditor.putBoolean("isSavePsd", rememberPsdBox.isChecked());
loginEditor.putBoolean("isAutoLogin", autoLoginBox.isChecked());
accessEditor.putInt("count", ++count); // 补充完整-3:loginEditor的commit方法提交数据
loginEditor.commit();
MainActivity.this.setContentView(R.layout.activity_welcome);
userInfo = (TextView) findViewById(R.id.userInfo);
userInfo.setText("欢迎您:" + name.getText().toString() + ",登陆成功!");
}
});
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)