his is working in android studio . main activity package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import android.widget.CompoundButton; public class MainActivity extends AppCompatActivity { private SharedPreferences sharedpreferences; private Editor editor; private CheckBox check; private boolean flag; private EditText edit1; private EditText edit2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit1 = (EditText) findViewById(R.id.editTextTextEmailAddress); edit2 = (EditText) findViewById(R.id.editTextNumberPassword); Button btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = edit1.getText().toString(); String userPassword = edit2.getText().toString(); if (check.isChecked()) { // If the "remember password" checkbox is checked, save the username and password to shared preferences editor.putString("account", userName); editor.putString("password", userPassword); editor.commit(); } if (userName.equals(sharedpreferences.getString("account", "")) && userPassword.equals(sharedpreferences.getString("password", ""))) { openActivity2(); } else { Toast.makeText(MainActivity.this, "Wrong username and password", Toast.LENGTH_SHORT).show(); } } }); // Retrieve shared preferences and editor sharedpreferences = getSharedPreferences("test", MODE_PRIVATE); editor = sharedpreferences.edit(); // Retrieve the checkbox and set a listener check = (CheckBox) findViewById(R.id.checkBox1); check.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // Save the checked state of the checkbox to shared preferences editor.putBoolean("rememberPassword", arg1); editor.commit(); } }); // Retrieve the rememberPassword flag from shared preferences flag = sharedpreferences.getBoolean("rememberPassword", false); // If rememberPassword flag is set, autofill the email and password fields if (flag) { edit1.setText(sharedpreferences.getString("account", "")); edit2.setText(sharedpreferences.getString("password", "")); check.setChecked(true); } } private void openActivity2() { Intent intent=new Intent(this, PlayerActivity2.class); startActivity(intent);
this is working in android studio .
main activity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedpreferences;
private Editor editor;
private CheckBox check;
private boolean flag;
private EditText edit1;
private EditText edit2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1 = (EditText) findViewById(R.id.editTextTextEmailAddress);
edit2 = (EditText) findViewById(R.id.editTextNumberPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = edit1.getText().toString();
String userPassword = edit2.getText().toString();
if (check.isChecked()) {
// If the "remember password" checkbox is checked, save the username and password to shared preferences
editor.putString("account", userName);
editor.putString("password", userPassword);
editor.commit();
}
if (userName.equals(sharedpreferences.getString("account", "")) && userPassword.equals(sharedpreferences.getString("password", ""))) {
openActivity2();
} else {
Toast.makeText(MainActivity.this, "Wrong username and password", Toast.LENGTH_SHORT).show();
}
}
});
// Retrieve shared preferences and editor
sharedpreferences = getSharedPreferences("test", MODE_PRIVATE);
editor = sharedpreferences.edit();
// Retrieve the checkbox and set a listener
check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// Save the checked state of the checkbox to shared preferences
editor.putBoolean("rememberPassword", arg1);
editor.commit();
}
});
// Retrieve the rememberPassword flag from shared preferences
flag = sharedpreferences.getBoolean("rememberPassword", false);
// If rememberPassword flag is set, autofill the email and password fields
if (flag) {
edit1.setText(sharedpreferences.getString("account", ""));
edit2.setText(sharedpreferences.getString("password", ""));
check.setChecked(true);
}
}
private void openActivity2() {
Intent intent=new Intent(this, PlayerActivity2.class);
startActivity(intent);
}
}
-----
Where do I set one or some correct passwords so that it can judge whether it is true or false?
For example, the correct default password is admin@123.com, and the password is 1234. If it does not match, it is wrong.
Step by step
Solved in 2 steps