Java 在if-else语句中使用pref.contains
除Java 在if-else语句中使用pref.contains,java,android,sharedpreferences,Java,Android,Sharedpreferences,除timid方程外,当前代码对所有情况都能正常工作。如果我在saa_charisma中没有勾选任何内容,那么等式保持不变(就像它应该的那样)。如果我检查sanguine,等式将最终值一分为二(就像它应该的那样)。如果我勾选timid,等式保持不变(好像它不应该) 这只虫子快把我逼疯了。我似乎无法独自解决这个问题。如果有人能提供任何帮助,我将不胜感激:) } 以下是将密钥保存到SharedReferences的活动中的代码: protected void onCreate(Bundle saved
timid
方程外,当前代码对所有情况都能正常工作。如果我在saa_charisma
中没有勾选任何内容,那么等式保持不变(就像它应该的那样)。如果我检查sanguine,等式将最终值一分为二(就像它应该的那样)。如果我勾选timid,等式保持不变(好像它不应该)
这只虫子快把我逼疯了。我似乎无法独自解决这个问题。如果有人能提供任何帮助,我将不胜感激:)
}
以下是将密钥保存到SharedReferences的活动中的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flaws_and_favors);
pref = getSharedPreferences("Character1", Context.MODE_PRIVATE);
feeble = (CheckBox) findViewById(R.id.feeble);
clumsy = (CheckBox) findViewById(R.id.clumsy);
simple = (CheckBox) findViewById(R.id.simple);
heedless = (CheckBox) findViewById(R.id.heedless);
unfocused = (CheckBox) findViewById(R.id.unfocused);
timid = (CheckBox) findViewById(R.id.timid);
foolish = (CheckBox) findViewById(R.id.foolish);
sickly = (CheckBox) findViewById(R.id.sickly);
mighty = (CheckBox) findViewById(R.id.mighty);
quicksilver = (CheckBox) findViewById(R.id.quicksilver);
genius = (CheckBox) findViewById(R.id.genius);
intuitive = (CheckBox) findViewById(R.id.intuitive);
focused = (CheckBox) findViewById(R.id.focused);
sanguine = (CheckBox) findViewById(R.id.sanguine);
crafty = (CheckBox) findViewById(R.id.crafty);
tough = (CheckBox) findViewById(R.id.tough);
feeble.setChecked(pref.getBoolean("feeble", false));
clumsy.setChecked(pref.getBoolean("clumsy", false));
simple.setChecked(pref.getBoolean("simple", false));
heedless.setChecked(pref.getBoolean("heedless", false));
unfocused.setChecked(pref.getBoolean("unfocused", false));
timid.setChecked(pref.getBoolean("timid", false));
foolish.setChecked(pref.getBoolean("foolish", false));
sickly.setChecked(pref.getBoolean("sickly", false));
mighty.setChecked(pref.getBoolean("mighty", false));
quicksilver.setChecked(pref.getBoolean("quicksilver", false));
genius.setChecked(pref.getBoolean("genius", false));
intuitive.setChecked(pref.getBoolean("intuitive", false));
focused.setChecked(pref.getBoolean("focused", false));
sanguine.setChecked(pref.getBoolean("sanguine", false));
crafty.setChecked(pref.getBoolean("crafty", false));
tough.setChecked(pref.getBoolean("tough", false));
}
public void onBackPressed()
{
Intent intent=new Intent (this, CharacterCreationSheet1.class);
startActivity(intent);
SharedPreferences.Editor editor=pref.edit();
if(feeble.isChecked()) {
editor.putBoolean("feeble", true);
} else {
editor.putBoolean("feeble", false);
}
if(clumsy.isChecked()) {
editor.putBoolean("clumsy", true);
} else {
editor.putBoolean("clumsy", false);
}
if(simple.isChecked()) {
editor.putBoolean("simple", true);
} else {
editor.putBoolean("simple", false);
}
if(heedless.isChecked()) {
editor.putBoolean("heedless", true);
} else {
editor.putBoolean("heedless", false);
}
if(unfocused.isChecked()) {
editor.putBoolean("unfocused", true);
} else {
editor.putBoolean("unfocused", false);
}
if(timid.isChecked()) {
editor.putBoolean("timid", true);
} else {
editor.putBoolean("timid", false);
}
if(foolish.isChecked()) {
editor.putBoolean("foolish", true);
} else {
editor.putBoolean("foolish", false);
}
if(sickly.isChecked()) {
editor.putBoolean("sickly", true);
} else {
editor.putBoolean("sickly", false);
}
if(mighty.isChecked()) {
editor.putBoolean("mighty", true);
} else {
editor.putBoolean("mighty", false);
}
if(quicksilver.isChecked()) {
editor.putBoolean("quicksilver", true);
} else {
editor.putBoolean("quicksilver", false);
}
if(genius.isChecked()) {
editor.putBoolean("genius", true);
} else {
editor.putBoolean("genius", false);
}
if(intuitive.isChecked()) {
editor.putBoolean("intuitive", true);
} else {
editor.putBoolean("intuitive", false);
}
if(focused.isChecked()) {
editor.putBoolean("focused", true);
} else {
editor.putBoolean("focused", false);
}
if(sanguine.isChecked()) {
editor.putBoolean("sanguine", true);
} else {
editor.putBoolean("sanguine", false);
}
if(crafty.isChecked()) {
editor.putBoolean("crafty", true);
} else {
editor.putBoolean("crafty", false);
}
if(tough.isChecked()) {
editor.putBoolean("tough", true);
} else {
editor.putBoolean("tough", false);
}
editor.commit();
}
}
您的假设是正确的,这是如何读取布尔值
boolean timid= prefs.getBoolean("timid", false);
if(prefs.getBoolean("timid", false))
{
}
else{
}
我对此很熟悉。但是,我该如何在if-else语句中实现这一点呢?我希望这对您有所帮助,这仍然会导致使用
sanguine
等式。未更改是否选中任何复选框。我用新的代码更新了OP卸载应用程序并重新安装应用程序,让我知道它是正确的wokring,然后它决定停止。我没有错误代码,但OP中的代码仅适用于sanguine。如果我检查timid它什么也不做,如果我检查nothing它什么也不做(就像它应该做的那样),如果我检查sanguine它将值减半(就像它应该做的那样)
boolean timid= prefs.getBoolean("timid", false);
if(prefs.getBoolean("timid", false))
{
}
else{
}