From a3c88e7642d578adb3d4e666efd3c9eb0e278bc6 Mon Sep 17 00:00:00 2001 From: zhenghaoyuan20 <735827001@qq.com> Date: Mon, 26 Jun 2023 15:22:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E6=88=B7=E4=BF=9D=E5=AD=98=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/sixaunyi/LoginActivity.java | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/sixaunyi/app/src/main/java/com/example/sixaunyi/LoginActivity.java b/src/sixaunyi/app/src/main/java/com/example/sixaunyi/LoginActivity.java index d98e699..ae0b710 100644 --- a/src/sixaunyi/app/src/main/java/com/example/sixaunyi/LoginActivity.java +++ b/src/sixaunyi/app/src/main/java/com/example/sixaunyi/LoginActivity.java @@ -29,16 +29,16 @@ public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle 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); // 获取输入框和按钮的实例 mUsername = findViewById(R.id.username); mPassword = findViewById(R.id.password); mLoginButton = findViewById(R.id.login); 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); mPassword.setText(password); Account_remember.setChecked(isChecked); @@ -46,25 +46,6 @@ public class LoginActivity extends AppCompatActivity { mLoginButton.setOnClickListener(v -> { 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() { String username = mUsername.getText().toString(); @@ -73,6 +54,13 @@ public class LoginActivity extends AppCompatActivity { if (if_correct) { SharedPreferences.Editor editor = sharedPreferences.edit(); 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(); Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class);