我目前正在嘗試編寫一個應用程序來計算一個人(男性/女性)所需的BMI和卡路里。 我的應用有2個部分組成: 需要輸出無法顯示在UI
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);
}
}
});
}
放置您的代碼日誌或調試點以調試錯誤點。 –
「不能按預期顯示結果」這是什麼意思?它是顯示錯誤的輸出還是顯示什麼都沒有? –
ya..it在分配給它的textview位置處什麼也沒有顯示。 – Onered