0

我有点疑惑。。

我有一个名为“结果”的变量,它是先前计算的变量的结果,然后附加 +10%、+20%、0%(与它相同..)-10% 等等,具体取决于哪个收音机用户选择的按钮,麻烦的是只有当用户点击按钮时才会计算数据。到目前为止,我的按钮工作正常:

public void onClick(View v) {
        switch (v.getId()) {

        case R.id.btnCalcCalories:
            double weight = Integer.parseInt(etWeight.getText().toString());
            double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
            ;
            lbm = weight * (100 - bodyfat) / 100;
            bmr = 370 + (21.6 * lbm);
            maintCals = bmr * actLevel;

            maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));
            lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
                    + "Kg");
            bmrResult.setText("Your Base Metabolic rate is "
                    + String.valueOf(bmr) + " calories");
            calorieResult.setText("Your Daily calorie goal is " + String.valueOf(calResult) + " calories");

虽然我需要使用这样的无线电组来获取用户是否希望减轻/增加/保持体重:

public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
        case R.id.radLW:
            setGPercentage = 10 * maintCals/100;
            calResult = maintCals - setGPercentage;
            break;
        case R.id.radLWF:
            setGPercentage = 20 * maintCals/100;
            calResult = maintCals - setGPercentage;
            break;
        case R.id.radM:
            setGPercentage = 0;
            break;
        case R.id.radGW:
            setGPercentage = 10 * maintCals/100;
            calResult = maintCals + setGPercentage;
            break;
        case R.id.radGWF:
            setGPercentage = 20 * maintCals/100;
            calResult = maintCals + setGPercentage;
            break;
        }

为了澄清,我需要根据用户选择的内容来计算“calResult”,显然如果变量仅在 onClick 方法期间计算,这可能是一个问题:/

4

2 回答 2

1

最后的广播组:

public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
        case R.id.radLW:
            setGPercentage = 10 * maintCals/100;
            finalGPercentage = maintCals - setGPercentage;
            break;
        case R.id.radLWF:
            setGPercentage = 20 * maintCals/100;
            finalGPercentage = maintCals - setGPercentage;
            break;
        case R.id.radM:
            setGPercentage = 0;
            break;
        case R.id.radGW:
            setGPercentage = 10 * maintCals/100;
            finalGPercentage = maintCals + setGPercentage;
            break;
        case R.id.radGWF:
            setGPercentage = 20 * maintCals/100;
            finalGPercentage = maintCals + setGPercentage;
            break;
        }

和点击:

case R.id.btnCalcCalories:
            double weight = Integer.parseInt(etWeight.getText().toString());
            double bodyfat = Integer.parseInt(etBodyfat.getText().toString());
            ;
            lbm = weight * (100 - bodyfat) / 100;
            bmr = 370 + (21.6 * lbm);
            maintCals = bmr * actLevel;

            maintCalories.setText("Calories need to maintain current bodyweight: " + String.valueOf(maintCals));

            lbmResult.setText("Your Lean Body Mass is " + String.valueOf(lbm)
                    + "Kg");
            bmrResult.setText("Your Base Metabolic rate is "
                    + String.valueOf(bmr) + " calories");
            calorieResult.setText("Your Daily calorie goal is " + String.valueOf(finalGPercentage) + " calories");
            break;

我只需要另一个变量来保存计算:D

于 2013-05-07T16:55:30.550 回答
0

您的问题并不完全清楚,但我认为您需要为您的活动设置某种初始化,因此calResult在用户进行选择之前您有一个计算的(默认)值。

查看Activity该类的文档以更好地了解Activty生命周期以及与初始化相关的方法,例如onCreate.

于 2013-05-07T16:48:23.757 回答