0

我目前正在尝试编写一个应用程序来计算一个人(男性/女性)所需的 BMI 和卡路里。我的应用程序有 2 个部分: 1. BMI 计算 2. 所需卡路里

第一部分效果很好(所以我排除了这部分的代码),但是对于第二部分,需要卡路里的计算,它无法按预期显示结果(在我点击“需要的卡路里”按钮后)。可能仍然缺少某些东西,但到目前为止我找不到它。

代码中的一切看起来都很好,没有错误。有谁能帮忙看看吗?:) 提前致谢。

代码如下:

package com.example.caloriescalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;

public class BMIcalculation extends Activity
{
EditText weightE;
EditText heightE ;
EditText ageE ;
TextView caloriesresult ;
RadioButton male; 
RadioButton female;

Button calories;

EditText weightText ;
EditText heightText ;
EditText ageText;
TextView resultText;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bmilayout_main);     

    weightE = (EditText)findViewById(R.id.weightText);
    heightE = (EditText)findViewById(R.id.heightText);
    ageE = (EditText)findViewById(R.id.ageText);
    caloriesresult = (TextView)findViewById(R.id.caloriesText);
    male = (RadioButton) findViewById(R.id.maleradio); 
    female = (RadioButton) findViewById(R.id.femaleradio);

    Button calories = (Button) findViewById(R.id.caloriesButton); 
    calories.setOnClickListener(new OnClickListener() 
       {
         @Override
                public void onClick(View arg0) 
                {    
                    int caloriesneed = 0, weight = 0, height = 0, age = 0;

                    try {
                            if ((!weightE.equals("")) && (!heightE.equals("")) && (!ageE.equals(""))) 
                                {
                                weightE = (EditText) findViewById(R.id.weightText);  
                                weight = Integer.parseInt(weightE.getText().toString().trim());     
                                heightE = (EditText) findViewById(R.id.heightText);  
                                height = Integer.parseInt(heightE.getText().toString().trim());
                                ageE = (EditText) findViewById(R.id.ageText);  
                                age = Integer.parseInt(ageE.getText().toString().trim());

                                    if (male.isSelected()) 
                                        {
                                            caloriesneed = (int) Math.round (655 + 9.6*weight + 1.8*height - 4.7*age);                                       
                                            caloriesresult.setText(caloriesneed);
                                        }

                                    else if (female.isSelected())
                                        {
                                            caloriesneed = (int) Math.round (66 + 13.7*weight + 5*height - 6.8*age);
                                            caloriesresult.setText(caloriesneed);
                                        }
                                }
                         } 
                     catch (Exception k)
                     { System.out.println(k);

                     }

                }
       });
}
4

2 回答 2

0

我想这是因为您尝试输入一个settext()需要stringfor 输入参数的整数,所以您可以尝试:

caloriesresult.setText("" + caloriesneed);

或者

caloriesresult.setText(Integer.toString(caloriesneed));

或者

caloriesresult.setText(String.valueOf(caloriesneed));
于 2013-11-01T09:22:22.183 回答
0

卡路里结果.setText(卡路里需要);

这是问题。在 Android 中,如果您将纯 int 变量分配给某个 TextWidget,则会引发错误,因为 Android会将其解释为strings.xml中存储的 String 的资源 id 。

因此,出于这个原因,您需要显式转换。您可以通过专注或使用String.valueOf(int value)orInteger.toString(int value)方法来实现它:

textWidget.setText(Integer.toString(value)); // best approach
textWidget.setText(String.valueOf(value));
textWidget.setText("" + value);

注意:这是绝对正常的行为,因为如果您正在开发商业应用程序,通常您希望支持更多语言 - 而此功能需要将本地化字符串存储在适当的XML文件中。

于 2013-11-01T09:25:28.550 回答