package com.example.app1; import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity2 extends AppCompatActivity { private EditText username; private EditText password; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_login); username = findViewById(R.id.username); password = findViewById(R.id.password); login = findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strUsername = username.getText().toString().trim(); String strPassword = password.getText().toString().trim(); // 从SharedPreferences中获取保存的账号和密码 SharedPreferences sharedPreferences = getSharedPreferences("user_mes", MODE_PRIVATE); String savedUsername = sharedPreferences.getString("username", ""); String savedPassword = sharedPreferences.getString("password", ""); if (strUsername.equals(savedUsername) && strPassword.equals(savedPassword)) { Toast.makeText(MainActivity2.this, "恭喜,登录成功!", Toast.LENGTH_SHORT).show(); Intent intent1 = new Intent(MainActivity2.this, IndexActivity.class); startActivity(intent1); } else { Toast.makeText(MainActivity2.this, "用户名或密码错误!", Toast.LENGTH_SHORT).show(); } } }); } //点击注册按钮时,跳转到注册界面 public void zhuce(View view) { Intent intent2 = new Intent(MainActivity2.this, MainActivity3.class); startActivity(intent2); } }