账户保存算法优化

zhenghaoyuan_branch
zhenghaoyuan20 2 years ago
parent 1118626ed3
commit a3c88e7642

@ -29,16 +29,16 @@ public class LoginActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences("account", Context.MODE_PRIVATE);
String username = sharedPreferences.getString("username", "");
String password =sharedPreferences.getString("password", "");
boolean isChecked = sharedPreferences.getBoolean("checkbox_state", false);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
// 获取输入框和按钮的实例 // 获取输入框和按钮的实例
mUsername = findViewById(R.id.username); mUsername = findViewById(R.id.username);
mPassword = findViewById(R.id.password); mPassword = findViewById(R.id.password);
mLoginButton = findViewById(R.id.login); mLoginButton = findViewById(R.id.login);
Account_remember=findViewById(R.id.remember); Account_remember=findViewById(R.id.remember);
sharedPreferences = getSharedPreferences("account", Context.MODE_PRIVATE);
String username = sharedPreferences.getString("username", "");
String password =sharedPreferences.getString("password", "");
boolean isChecked = sharedPreferences.getBoolean("checkbox_state", false);
mUsername.setText(username); mUsername.setText(username);
mPassword.setText(password); mPassword.setText(password);
Account_remember.setChecked(isChecked); Account_remember.setChecked(isChecked);
@ -46,25 +46,6 @@ public class LoginActivity extends AppCompatActivity {
mLoginButton.setOnClickListener(v -> { mLoginButton.setOnClickListener(v -> {
attemptLogin(); attemptLogin();
}); });
Account_remember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 复选框状态改变时的逻辑处理
String password = mPassword.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isChecked) {
// 复选框被选中
editor.putBoolean("checkbox_state", true);
editor.putString("password", password);
editor.apply();
} else {
// 复选框未被选中
editor.putBoolean("checkbox_state", false);
editor.putString("password", "");
editor.apply();
}
}
});
} }
private void attemptLogin() { private void attemptLogin() {
String username = mUsername.getText().toString(); String username = mUsername.getText().toString();
@ -73,6 +54,13 @@ public class LoginActivity extends AppCompatActivity {
if (if_correct) { if (if_correct) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", username); editor.putString("username", username);
editor.putBoolean("checkbox_state", Account_remember.isChecked());
editor.apply();
if (Account_remember.isChecked()){
editor.putString("password", password);
}else{
editor.putString("password", "");
}
editor.apply(); editor.apply();
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class); Intent intent = new Intent(LoginActivity.this, MainActivity.class);

Loading…
Cancel
Save