在if-else语句中使用pref.contains

时间:2014-10-06 03:16:59

标签: java android sharedpreferences

当前代码适用于除timid等式以外的所有内容。如果我在saa_charisma中没有检查任何内容,那么等式保持不变(就像它应该的那样)。如果我检查乐观,那么等式将最终值分成两半(就像它应该的那样)。如果我检查胆怯,方程式保持不变(就像它不应该)。

这个错误一直让我发疯。我似乎无法自己解决这个问题。如果有人可以提供任何帮助,我将非常感激:)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.saa_charisma);
    pref = getSharedPreferences("Character1", Context.MODE_PRIVATE);

    fpr = (TextView) findViewById(R.id.fpr);

    charismafpvalue = (TextView) findViewById(R.id.charismafpvalue);
    persuasionfpvalue = (TextView) findViewById(R.id.persuasionfpvalue);
    elicitationfpvalue = (TextView) findViewById(R.id.elicitationfpvalue);
    animalhandlingfpvalue = (TextView) findViewById(R.id.animalhandlingfpvalue);

    charisma = (EditText) findViewById(R.id.charisma);
    persuasion = (EditText) findViewById(R.id.persuasion);
    elicitation = (EditText) findViewById(R.id.elicitation);
    animalhandling = (EditText) findViewById(R.id.animalhandling);

    getcharisma = pref.getString("charisma", "");
    getpersuasion = pref.getString("persuasion", "");
    getelicitation = pref.getString("elicitation", "");
    getanimalhandling = pref.getString("animalhandling", "");

    charisma.setText(getcharisma);
    persuasion.setText(getpersuasion);
    elicitation.setText(getelicitation);
    animalhandling.setText(getanimalhandling);

    float CHRV = NumberUtils.toFloat(charisma.getText().toString(), 0.0f);
    float PERV = NumberUtils.toFloat(persuasion.getText().toString(), 0.0f);
    persuasionfpvalue.setText(Float.toString((PERV + 1) * PERV));
    float ELIV = NumberUtils.toFloat(elicitation.getText().toString(), 0.0f);
    elicitationfpvalue.setText(Float.toString((ELIV + 1) * ELIV));
    float AMLV = NumberUtils.toFloat(animalhandling.getText().toString(), 0.0f);
    animalhandlingfpvalue.setText(Float.toString((AMLV + 1) * AMLV));

    if (pref.getBoolean("timid", true)) {
        charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV) * 2));
    } else if (pref.getBoolean("timid", false)){
        charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
    } else {
        charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
    }

    if (pref.getBoolean("sanguine", true)) {
        charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV) / 2));
    } else if (pref.getBoolean("sanguine", false)){
        charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
    } else {
        charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
    }   

    float FP = NumberUtils.toFloat(pref.getString("fp", ""));
    float HLTH = NumberUtils.toFloat(pref.getString("healthfpvalue", ""));
    float CHR = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f);
    float PER = NumberUtils.toFloat(persuasionfpvalue.getText().toString(), 0.0f);
    float ELI = NumberUtils.toFloat(elicitationfpvalue.getText().toString(), 0.0f);
    float AML = NumberUtils.toFloat(animalhandlingfpvalue.getText().toString(), 0.0f);
    float MAN = NumberUtils.toFloat(pref.getString("manipulationfpvalue", ""));
    float ACT = NumberUtils.toFloat(pref.getString("actingfpvalue", ""));
    float SED = NumberUtils.toFloat(pref.getString("seductionfpvalue", ""));
    float INTM = NumberUtils.toFloat(pref.getString("intimidationfpvalue", ""));
    float INT = NumberUtils.toFloat(pref.getString("intelligencefpvalue", ""));
    float TRP = NumberUtils.toFloat(pref.getString("trappingfpvalue", ""));
    float FOR = NumberUtils.toFloat(pref.getString("forgingfpvalue", ""));
    float CON = NumberUtils.toFloat(pref.getString("concoctionsfpvalue", ""));
    float DEX = NumberUtils.toFloat(pref.getString("dexterityfpvalue", ""));
    float ACR = NumberUtils.toFloat(pref.getString("acrobaticsfpvalue", ""));
    float DOD = NumberUtils.toFloat(pref.getString("dodgingfpvalue", ""));
    float RC = NumberUtils.toFloat(pref.getString("rangedfpvalue", ""));
    float STR = NumberUtils.toFloat(pref.getString("strengthfpvalue", ""));
    float MEL = NumberUtils.toFloat(pref.getString("meleefpvalue", ""));
    float H2H = NumberUtils.toFloat(pref.getString("h2hfpvalue", ""));
    float PRY = NumberUtils.toFloat(pref.getString("parryfpvalue", ""));
    float WIT = NumberUtils.toFloat(pref.getString("witsfpvalue", ""));
    float STL = NumberUtils.toFloat(pref.getString("stealthfpvalue", ""));
    float WILD = NumberUtils.toFloat(pref.getString("wildernessfpvalue", ""));
    float TRA = NumberUtils.toFloat(pref.getString("trackingfpvalue", ""));
    float QI = NumberUtils.toFloat(pref.getString("qifpvalue", ""));
    float FIRE = NumberUtils.toFloat(pref.getString("firefpvalue", ""));
    float WAT = NumberUtils.toFloat(pref.getString("waterfpvalue", ""));
    float EAR = NumberUtils.toFloat(pref.getString("earthfpvalue", ""));
    float AIRV = NumberUtils.toFloat(pref.getString("airfpvalue", ""));

    fpr.setText(Float.toString(FP - (QI + FIRE + WAT + EAR + AIRV + WIT + STL + WILD + TRA + STR + MEL + H2H + 
            PRY + MAN + ACT + SED + INTM + INT + TRP + FOR + CON + HLTH + CHR + PER + ELI + AML + DEX + ACR + DOD + RC)));

//  TEXT WATCHERS
    TextWatcher watcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {

            float CHRV = NumberUtils.toFloat(charisma.getText().toString(), 0.0f);
            float PERV = NumberUtils.toFloat(persuasion.getText().toString(), 0.0f);
            persuasionfpvalue.setText(Float.toString((PERV + 1) * PERV));
            float ELIV = NumberUtils.toFloat(elicitation.getText().toString(), 0.0f);
            elicitationfpvalue.setText(Float.toString((ELIV + 1) * ELIV));
            float AMLV = NumberUtils.toFloat(animalhandling.getText().toString(), 0.0f);
            animalhandlingfpvalue.setText(Float.toString((AMLV + 1) * AMLV));

            if (pref.getBoolean("timid", true)) {
                charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV) * 2));
            } else if (pref.getBoolean("timid", false)){
                charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
            } else {
                charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
            }

            if (pref.getBoolean("sanguine", true)) {
                charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV) / 2));
            } else if (pref.getBoolean("sanguine", false)){
                charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
            } else {
                charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV));
            }   

            float FP = NumberUtils.toFloat(pref.getString("fp", ""));
            float HLTH = NumberUtils.toFloat(pref.getString("healthfpvalue", ""));
            float CHR = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f);
            float PER = NumberUtils.toFloat(persuasionfpvalue.getText().toString(), 0.0f);
            float ELI = NumberUtils.toFloat(elicitationfpvalue.getText().toString(), 0.0f);
            float AML = NumberUtils.toFloat(animalhandlingfpvalue.getText().toString(), 0.0f);
            float MAN = NumberUtils.toFloat(pref.getString("manipulationfpvalue", ""));
            float ACT = NumberUtils.toFloat(pref.getString("actingfpvalue", ""));
            float SED = NumberUtils.toFloat(pref.getString("seductionfpvalue", ""));
            float INTM = NumberUtils.toFloat(pref.getString("intimidationfpvalue", ""));
            float INT = NumberUtils.toFloat(pref.getString("intelligencefpvalue", ""));
            float TRP = NumberUtils.toFloat(pref.getString("trappingfpvalue", ""));
            float FOR = NumberUtils.toFloat(pref.getString("forgingfpvalue", ""));
            float CON = NumberUtils.toFloat(pref.getString("concoctionsfpvalue", ""));
            float DEX = NumberUtils.toFloat(pref.getString("dexterityfpvalue", ""));
            float ACR = NumberUtils.toFloat(pref.getString("acrobaticsfpvalue", ""));
            float DOD = NumberUtils.toFloat(pref.getString("dodgingfpvalue", ""));
            float RC = NumberUtils.toFloat(pref.getString("rangedfpvalue", ""));
            float STR = NumberUtils.toFloat(pref.getString("strengthfpvalue", ""));
            float MEL = NumberUtils.toFloat(pref.getString("meleefpvalue", ""));
            float H2H = NumberUtils.toFloat(pref.getString("h2hfpvalue", ""));
            float PRY = NumberUtils.toFloat(pref.getString("parryfpvalue", ""));
            float WIT = NumberUtils.toFloat(pref.getString("witsfpvalue", ""));
            float STL = NumberUtils.toFloat(pref.getString("stealthfpvalue", ""));
            float WILD = NumberUtils.toFloat(pref.getString("wildernessfpvalue", ""));
            float TRA = NumberUtils.toFloat(pref.getString("trackingfpvalue", ""));
            float QI = NumberUtils.toFloat(pref.getString("qifpvalue", ""));
            float FIRE = NumberUtils.toFloat(pref.getString("firefpvalue", ""));
            float WAT = NumberUtils.toFloat(pref.getString("waterfpvalue", ""));
            float EAR = NumberUtils.toFloat(pref.getString("earthfpvalue", ""));
            float AIRV = NumberUtils.toFloat(pref.getString("airfpvalue", ""));

            fpr.setText(Float.toString(FP - (QI + FIRE + WAT + EAR + AIRV + WIT + STL + WILD + TRA + STR + MEL + H2H + 
                    PRY + MAN + ACT + SED + INTM + INT + TRP + FOR + CON + HLTH + CHR + PER + ELI + AML + DEX + ACR + DOD + RC)));

            SharedPreferences.Editor editor=pref.edit();                
            editor.putString("charisma", charisma.getText().toString());
            editor.putString("persuasion", persuasion.getText().toString());
            editor.putString("animalhandling", animalhandling.getText().toString());
            editor.putString("elicitation", elicitation.getText().toString());

            editor.putString("charismafpvalue", charismafpvalue.getText().toString());
            editor.putString("persuasionfpvalue", persuasionfpvalue.getText().toString());
            editor.putString("animalhandlingfpvalue", animalhandlingfpvalue.getText().toString());
            editor.putString("elicitationfpvalue", elicitationfpvalue.getText().toString());
            editor.commit();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
    };

    charisma.addTextChangedListener(watcher);
    persuasion.addTextChangedListener(watcher);
    elicitation.addTextChangedListener(watcher);
    animalhandling.addTextChangedListener(watcher); 

}

@Override
public void onBackPressed()
{
    Intent intent = new Intent (this, NewSkillsAndAttributes.class);
        startActivity(intent);

        SharedPreferences.Editor editor=pref.edit();                
        editor.putString("charisma", charisma.getText().toString());
        editor.putString("persuasion", persuasion.getText().toString());
        editor.putString("animalhandling", animalhandling.getText().toString());
        editor.putString("elicitation", elicitation.getText().toString());

        editor.putString("charismafpvalue", charismafpvalue.getText().toString());
        editor.putString("persuasionfpvalue", persuasionfpvalue.getText().toString());
        editor.putString("animalhandlingfpvalue", animalhandlingfpvalue.getText().toString());
        editor.putString("elicitationfpvalue", elicitationfpvalue.getText().toString());
        editor.commit();
}

}

以下是将密钥保存到SharedPreferences的活动中的代码:

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

1 个答案:

答案 0 :(得分:0)

你的假设是正确的,这是如何读取布尔值

boolean  timid= prefs.getBoolean("timid", false);

if(prefs.getBoolean("timid", false))
{
}
else{

}
相关问题