gpt4 book ai didi

java - 在 login.class 上验证时,来自共享首选项的数据不正确

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:31 24 4
gpt4 key购买 nike

登录.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();
}

}
}
<小时/>

注册.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)时,它将存储在共享首选项中,并将用作用户登录(Login.class)和进入MainPage()时输入的数据。

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

最佳答案

我发现您正在将 EditView 对象的值与实际的用户名/密码字符串进行比较。您也不希望将 Lname 和密码值用作“默认”值。在这种情况下,如果没有保存用户名/密码,它将始终识别为已登录。

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

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();
}
}

关于java - 在 login.class 上验证时,来自共享首选项的数据不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39978682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com