整数共享首选项始终返回0
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整数共享首选项始终返回0相关的知识,希望对你有一定的参考价值。
我正在尝试编写一个简单的密码检查来保护我的应用程序的设置。密码应保存在共享首选项中。当sharedPreference返回零时,main调用setPassword。这是为了确保在第一次尝试输入设置时设置密码。然后,setPassword应设置密码并在sharedPreference中将其保密。但是当检查是否设置了密码时,sharedPreference总是返回0。
这是我的主要内容:
final Intent intentSetPasswords = new Intent(this, SetPasswordActivity.class);
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);
//setting button on click listener
buttonSetings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//check if a password was set muss Überarbeitet werden
passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);
Log.e("password", ""+ passwordToCheck);
if(passwordToCheck==0){
startActivity(intentSetPasswords);
}else{
startActivity(new Intent(MainActivity.this, PasswordCheckActivity.class));
}
}
});
和tis是我的setPassword:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_password);
final Intent intentSettings = new Intent(this, SettingsActivity.class);
buttonSetPassword = (Button)findViewById(R.id.buttonSetPassword);
textViewSetPassword = (TextView)findViewById(R.id.textViewSetPasswordText);
editTextPassword = (EditText)findViewById(R.id.passwordNumericSettingsSetPassword);
final Intent intent = new Intent(this, MainActivity.class);
buttonSetPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counterButton++;
switch (counterButton){
case 1:
password = editTextPassword.getText().toString();
if (password.length() == 4){
Log.d("First Password", "taken");
buttonSetPassword.setText("Confirm Password!");
editTextPassword.setText("");
}else{
Log.d("First Password", "not taken");
textViewSetPassword.setText("The Password must be
four numbers long!");
password = "";
editTextPassword.setText("");
counterButton=0;
}
break;
case 2:
passwordToConfirm = editTextPassword.getText().toString();
if (Integer.parseInt(passwordToConfirm) == Integer.parseInt(password)){
Log.e("password", password);
Log.e("passwordToConfirm", passwordToConfirm);
SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("PASSWORDSETTINGS", Integer.parseInt(password));
editor.commit();
startActivity(intentSettings);
finish();
}else{
textViewSetPassword.setText("The passwords aren't the same!
Try again!");
buttonSetPassword.setText("Set Password");
editTextPassword.setText("");
passwordToConfirm = "";
password = "";
counterButton = 0;
startActivity(intent);
}
break;
}
}
});
}
我想sharedPreference存在问题,但我没有得到什么。
答案
首先,检查counterButton
值并确保执行case 2分支。其次,用于设置的sharedPreferences
和用于获取的sharedPreferences
应该是相同的。你可以这样做:
private final static String MyPREFERENCES = "MyPrefs"; //define two class fields
private SharedPreferences mSharedPreferences;
mSharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);//in activity Oncreate() methods.
//and set or get in other place as you want.
另一答案
首先,你得到像这样的共享偏好
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
稍后设置密码时,您将获得如下密码
SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);
他们是两个不同的共享偏好。使用第一个共享首选项。有关更多信息,请参阅此页https://developer.android.com/training/data-storage/shared-preferences
另一答案
轻松使用下面的库来管理数据
https://github.com/orhanobut/hawk
以上是关于整数共享首选项始终返回0的主要内容,如果未能解决你的问题,请参考以下文章