0

在下面的代码中,用户输入一组属性,然后需要存储它们,然后使用 CaluculateCalories() 计算它返回我也想存储在 sharedpreferences 中的卡路里数 (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 回答