0

以下のコードでは、ユーザーが一連の属性を入力し、それらを保存してからCaluculateCalories()を使用して計算する必要があります。これにより、共有設定にも保存するカロリー数(Calnum)が返されます。どうすればそれが可能ですか?これがコードの一部です。Savepreferencesにいくつかの行を追加する必要があります。方法がわかりません。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createprofile);
    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);

    name = (EditText) findViewById(R.id.namefield);
    name.setText(customSharedPreference.getString("namepref", ""));

    age = (EditText) findViewById(R.id.agefield);
    // int Age = Integer.parseInt(age.getText().toString());
    age.setText(customSharedPreference.getString("agepref", ""));

    genderradiogroup = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    genderradiogroup.check(customSharedPreference.getInt("genderprefs",
            rb_male.getId()));

    weight = (EditText) findViewById(R.id.weightfield);
    // int Weight = Integer.parseInt(weight.getText().toString());
    weight.setText(customSharedPreference.getString("weightpref", ""));

    height = (EditText) findViewById(R.id.heightfield);
    // int Height = Integer.parseInt(height.getText().toString());
    height.setText(customSharedPreference.getString("heightpref", ""));

    levelofactivity = (RadioGroup) findViewById(R.id.radioGroup3);
    rb_light = (RadioButton) findViewById(R.id.lightradiobutton);
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton);
    rb_heavy = (RadioButton) findViewById(R.id.heavyradiobutton);
    levelofactivity.check(customSharedPreference.getInt("levelpref",
            rb_light.getId()));

    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton);

    addUser.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            savePreferences();

        }
    });

}

private void savePreferences() {

    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = customSharedPreference.edit();
    editor.putString("namepref", name.getText().toString());
    editor.putString("agepref", age.getText().toString());
    editor.putInt("genderprefs", genderradiogroup.getCheckedRadioButtonId());
    editor.putString("heightpref", height.getText().toString());
    editor.putString("weightpref", weight.getText().toString());
    editor.putInt("levelpref", levelofactivity.getCheckedRadioButtonId());

    editor.commit();
    finish();

}


public float calculateCalories(int age, int weight, int height, String level) {

    if (rb_male.isChecked()) {

        bmr = 66.5f + (13.75f * weight) + (5.003f * height)
                - (6.755f * age);

        if (rb_light.isChecked()) {
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            calnum = bmr * 1.725f;
        }

    } else if (rb_female.isChecked()) {

        bmr = 665 + (9.563f * weight) + (1.850f * height) - (4.676f * age);

        if (rb_light.isChecked()) {
            calnum = bmr * 1.375f;
        }
        if (rb_moderate.isChecked()) {
            calnum = bmr * 1.55f;
        }
        if (rb_heavy.isChecked()) {
            calnum = bmr * 1.725f;
        }

    }
    return calnum;

}

}

4

1 に答える 1

1

他のSharedPreferenceと同じように設定します。何に問題がありますか?何を試しましたか?

于 2012-04-24T16:48:18.857 に答える