gpt4 book ai didi

android - 无法从 Android 中的 SharedPreferences 访问字符串

转载 作者:行者123 更新时间:2023-11-29 19:42:30 26 4
gpt4 key购买 nike

所以我有两个屏幕,设置密码屏幕和输入密码(登录)屏幕。如果没有密码(即从未使用过该应用程序),我希望它弹出输入密码屏幕,并在随后启动时转到登录屏幕。如果用户输入密码,它会与存储在 sharepreferences 中的值进行比较,如果正确则继续到菜单。但是使用set-password screen后,login screen似乎无法访问密码。

如何让登录界面看到注册页面设置的密码?

这是创建密码代码:

public class CreatePassword extends Activity {
public EditText setPass1, setPass2;
String newPass;

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

setPass1 = (EditText)findViewById(R.id.editTextSetPassword);
setPass2 = (EditText)findViewById(R.id.editTextRepeatSetPassword);
}

public void btnSubmitNewPassword(View v){
if(setPass1.getText().toString() != "") {
if (setPass1.getText().toString().equals(setPass2.getText().toString())) {
newPass = setPass1.getText().toString();

SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("sharedPref", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("password", newPass);
prefEditor.apply();
Toast.makeText(getApplicationContext(), newPass, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "New Password Set", Toast.LENGTH_LONG).show();
startActivity(new Intent(CreatePassword.this, Menu.class));
} else {
Toast.makeText(getApplicationContext(), "Passwords don't match!", Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), "Please Enter Desired Password", Toast.LENGTH_LONG).show();
}
}
}//end CreatePassword class

以及拒绝使用刚刚设置的密码的EnterPassword(登录)界面:

public class EnterPassword extends Activity {

EditText editTextPasswordAttempt;
SharedPreferences sharedPref;
String passwordAttempt, passwordActual;

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

sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean passwordExists = sharedPref.contains("password");

if(passwordExists == false){
startActivity(new Intent(EnterPassword.this, CreatePassword.class));
}

editTextPasswordAttempt = (EditText)findViewById(R.id.editTextPassword);
}

public void btnSubmitToMenu(View v){

passwordActual = sharedPref.getString("password", );
Toast.makeText(getApplicationContext(), passwordActual, Toast.LENGTH_LONG).show();
passwordAttempt = editTextPasswordAttempt.getText().toString();

if(passwordAttempt.equals(passwordActual)) {
startActivity(new Intent(EnterPassword.this, Menu.class));
}
else{
Toast.makeText(getApplicationContext(), "Invalid Password", Toast.LENGTH_LONG).show();
}
}
}

最佳答案

您正在使用 2 个不同的 SharedPreferences。

CreatePassword Activity 中,您执行以下操作:

getApplicationContext().getSharedPreferences("sharedPref", MODE_PRIVATE);

但在 EnterPassword Activity 中,您可以:

PreferenceManager.getDefaultSharedPreferences(this);

如果您在两个 Activity 中使用同一行,它应该可以工作。

关于android - 无法从 Android 中的 SharedPreferences 访问字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38413961/

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