this is a android app code ,is like this main.class 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 (userName.equals(sharedpreferences.getString("account", &..n@123.com")) && userPassword.equals(sharedpreferences.getString("password", "1234"))) { 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); } ---- xml ------ I hope to let it automatically log in next time after I check the remember password, enter the password. But I don't know why, after I checked Enter password after login, it didn't log in automatically. How to fix this problem.
this is a android app code ,is like this
main.class
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 (userName.equals(sharedpreferences.getString("account", &..n@123.com"))
&& userPassword.equals(sharedpreferences.getString("password", "1234"))) {
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);
}
----
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayerActivity2">
<TextView
android:id="@+id/textView9"
android:layout_width="379dp"
android:layout_height="49dp"
android:layout_marginStart="28dp"
android:text="Copyright purposes."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.863" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remember password"
tools:layout_editor_absoluteX="152dp"
tools:layout_editor_absoluteY="507dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
------
I hope to let it automatically log in next time after I check the remember password, enter the password.
But I don't know why, after I checked Enter password after login, it didn't log in automatically. How to fix this problem.
Step by step
Solved in 3 steps