2014-10-06 39 views
0

當前代碼對除timid等式以外的所有內容都正常工作。如果我在saa_charisma中查不出任何內容,那麼方程式保持不變(就像它應該那樣)。如果我檢查一下樂觀,那麼等式將最終值分成兩半(就像它應該那樣)。如果我檢查膽小,方程保持不變(就像它不應該)。在if-else語句中使用pref.contains

這個bug一直讓我發瘋。我似乎無法自己弄清楚。如果任何人都可以提供任何幫助,我會非常感激:)

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

回答

0

你的人正確的假設,這是如何閱讀布爾

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

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

} 
+0

我熟悉這一點。但是,我將如何執行到if-else語句中? – Mikitz06 2014-10-06 04:10:31

+0

我希望這可以幫助你 – Ranjithkumar 2014-10-06 04:47:49

+0

仍然會導致使用'sanguine'方程式。沒有任何更改是否選中任何複選框。我用新代碼更新了操作系統 – Mikitz06 2014-10-06 04:57:11