在 login.class 上验证时来自 sharedpreferences 的数据不正确

Data from sharedpreferences incorrect when validated on login.class

Login.class

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends AppCompatActivity {

    EditText Lname, Password;
    TextView Regit;
    Button Login, Cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Lname = (EditText) findViewById(R.id.txtLname);
        Password = (EditText) findViewById(R.id.txtPassword);
        Login = (Button) findViewById(R.id.btnLogin);
        Cancel = (Button) findViewById(R.id.btnCancel);
        Regit = (TextView) findViewById(R.id.txtRegit);

        Regit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Registration.class);
                startActivity(intent);
            }
        });

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Lname.setText("");
                Password.setText("");
            }
        });

        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
           load();
            }
        });
    }

    public void run() {
        Intent x = new Intent(Login.this, MainPage.class);
        startActivity(x);
    }

       public void LoginSuccess() {
        Toast.makeText(this, "Successfully Logged in!", Toast.LENGTH_SHORT).show();
        run();

    }


    public  void load(){


        final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = pref.edit();

        String value=(pref.getString("Name", Lname.getText().toString()));
        String value2=(pref.getString("Password", Password.getText().toString()));


        if (value.equals(Lname) & value2.equals(Password)){
            LoginSuccess();
        } else{
            Toast.makeText(this, "Last Name or Password Incorrect or Does not Exist!", Toast.LENGTH_SHORT).show();
        }

    }
}

Registration.class

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Pattern;


public class Registration extends AppCompatActivity {

    EditText Lname, Fname, Mname, BDate, Email, Password;
    String Lnamee, Fnamee, Mnamee, Bdatee, Emails, Passwords;
    TextView Login;
    Button Register, Cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        Lname = (EditText) findViewById(R.id.txtFamilyName);
        Fname = (EditText) findViewById(R.id.txtFirstName);
        Mname = (EditText) findViewById(R.id.txtMiddleName);
        BDate = (EditText) findViewById(R.id.txtBDay);
        Email = (EditText) findViewById(R.id.txtEMail);
        Password = (EditText) findViewById(R.id.txtPassword);
        Register = (Button) findViewById(R.id.btnRegister);
        Cancel = (Button) findViewById(R.id.btnCancel);
        Login = (TextView) findViewById(R.id.txtLogin);




        Login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Registration.this, Login.class);
                startActivity(intent);
            }
        });

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Lname.setText("");
                Fname.setText("");
                Mname.setText("");
                BDate.setText("");
                Email.setText("");
                Password.setText("");
            }
        });

        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                register();
            }
        });
    }

    public void register() {
        initialize();
        if (!validate()) {
            Toast.makeText(this, "Sign-up has Failed", Toast.LENGTH_SHORT).show();
        } else {
            onSignUpSuccess();
        }
    }

    public void onSignUpSuccess() {
        save();
        Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Registration.this, Login.class);
        startActivity(intent);

    }
    public boolean validate() {
        boolean valid = true;
        if (Lnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+").matcher(Lnamee).matches()) {
            Lname.setError("Enter letters only!");

            valid = false;
        }

        if (Mnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+ ").matcher(Mnamee).matches()) {
           Mname.setError("Enter letters only!");
            valid = false;
        }

        if (Fnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+").matcher(Fnamee).matches()) {
           Fname.setError("Enter letters only!");
            valid = false;
        }

       if (Emails.isEmpty() || Pattern.compile("[a-zA-Z0-9]" + "\@" + "[a-zA-Z]" + "\." +  "[a-zA-Z]").matcher(Emails).matches()){
            Email.setError("Enter valid e-mail address!");
            valid = false;
        }

        if (Passwords.isEmpty() || Passwords.length() < 8){
           Password.setError("Password must be 8 characters!");
            valid = false;
        }
        return valid;


    }

    public void initialize(){
        Lnamee = Lname.getText().toString().trim();
        Mnamee = Mname.getText().toString().trim();
        Fnamee = Fname.getText().toString().trim();
        Emails = Email.getText().toString().trim();
        Passwords = Password.getText().toString().trim();
    }

    public void save() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = pref.edit();

        String LastNameReg = Lname.getText().toString();
        String PWReg = Password.getText().toString();

        editor.putString("Name", LastNameReg);
        editor.putString("Password", PWReg);
        editor.commit();
    }

}

程序的预期行为是,从注册开始:当用户输入姓氏(txtFamilyName)和密码(txtPassword)时,它将存储在 sharedpreferences 中,并将用作用户登录(Login.class)和进入 MainPage() 时输入的数据。

在注册(Registration.class)期间,当我输入我的家庭或姓氏和密码,并在 Login.class 上使用它时,即使必填字段正确,它也没有进入主页.

我突然想到您正在将 EditView 对象的值与实际的用户名/密码字符串进行比较。您也不希望将 Lname 和 Password 值用作 "default" 值。在这种情况下,如果没有保存用户名/密码,它将始终标识为已登录。

下面所做的更改应该足够了。

public void load() {
    final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences.Editor editor = pref.edit();

    String value=(pref.getString("Name", ""));
    String value2=(pref.getString("Password", ""));

    if (value.equals(Lname.getText().toString()) & value2.equals(Password.getText().toString())){
        LoginSuccess();
    } else{
        Toast.makeText(this, "Last Name or Password Incorrect or Does not Exist!", Toast.LENGTH_SHORT).show();
    }
}