gpt4 book ai didi

java - (Android)试图理解为什么我的方法不会在 TextView 中计算/显示正确的值

转载 作者:行者123 更新时间:2023-11-30 02:13:16 26 4
gpt4 key购买 nike

在我的 Activity 中调用(计算 BMI)的方法应该计算和显示 BMI(基于代码中的公式),并在 TextView 中显示它的值。应用程序运行正常,但显示的值始终为零。我正在努力弄清楚问题是否出在我的公式中(不太可能),或者我传递值的方式是否不正确。任何帮助,将不胜感激。代码如下

.Java文件

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;


public class Health extends ActionBarActivity {
EditText bloodPressure;
EditText cholesterol;
int weight;
int height;
EditText heightString;
EditText age;
TextView bmiText;
EditText weightString;
MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
weightString = (EditText) findViewById(R.id.weightTextBox);

heightString =(EditText) findViewById(R.id.heightTextBox);

bmiText = (TextView) findViewById(R.id.bmiTextView);
bloodPressure = (EditText) findViewById(R.id.bloodPressureTextBox);
cholesterol = (EditText) findViewById(R.id.cholesterolTextBox);
age = (EditText) findViewById(R.id.ageTextBox);
dbHandler = new MyDBHandler(this, null, null, 1);
}

public void calculateBMI(View view)
{
weight = Integer.parseInt(weightString.getText().toString());
height = Integer.parseInt(heightString.getText().toString());
int BMI = 703 * (weight/(height*height));
bmiText.setText("Your calculated BMI is: " + Integer.toString(BMI));
Toast.makeText(Health.this, " Info Saved ", Toast.LENGTH_SHORT).show();
HealthInfo healthInfo = new HealthInfo(bloodPressure.getText().toString(),cholesterol.getText().toString(),heightString.getText().toString(),weightString.getText().toString(),age.getText().toString());
dbHandler.addHealth(healthInfo);

}

public void calorieClick(View view){
Intent i = new Intent(this, CalorieTracker.class);
startActivity(i);
}

public void openTips(View view){
Intent i = new Intent(this, HealthTipsActivity.class);
startActivity(i);
}

public void openPlans(View view){
Intent i = new Intent(this, DietPlansActivity.class);
startActivity(i);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_health, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

.XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="seniorproject.broncos.broncowellnessapp.Health"
android:background="#006699">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/blood_pressure_text"
android:id="@+id/bloodPressureTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/cholesterol_text"
android:id="@+id/cholesterolTextView"
android:layout_below="@+id/bloodPressureTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/weight_text"
android:id="@+id/weightTextView"
android:layout_below="@+id/cholesterolTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/height_text"
android:id="@+id/heightTextView"
android:layout_below="@+id/weightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/age_text"
android:id="@+id/ageTextView"
android:layout_below="@+id/heightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:width="150dp"
android:id="@+id/bloodPressureTextBox"
android:layout_below="@+id/bloodPressureTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="@+id/cholesterolTextBox"
android:layout_below="@+id/cholesterolTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="@+id/weightTextBox"
android:layout_below="@+id/weightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="@+id/heightTextBox"
android:layout_below="@+id/heightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="@+id/ageTextBox"
android:layout_below="@+id/ageTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/diet_plans_button_text"
android:onClick="openPlans"
android:id="@+id/dietPlansButton"
android:layout_below="@+id/ageTextBox"
android:layout_marginTop="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calorie_tracker_button"
android:id="@+id/calorieAccessButton"
android:layout_below="@+id/dietPlansButton"
android:onClick ="calorieClick"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tips_button_text"
android:id="@+id/tipsButton"
android:onClick="openTips"
android:layout_below="@+id/calorieAccessButton"
android:layout_alignRight="@+id/calorieAccessButton"
android:layout_alignEnd="@+id/calorieAccessButton" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BMI_button_text"
android:id="@+id/bmiButton"
android:onClick="calculateBMI"
android:layout_alignBottom="@+id/cholesterolTextBox"
android:layout_alignRight="@+id/dietPlansButton"
android:layout_alignEnd="@+id/dietPlansButton"
android:layout_marginRight="36dp"
android:layout_marginEnd="36dp"
android:background="#ff00b812" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Push Button to Compute BMI"
android:id="@+id/bmiTextView"
android:layout_alignBottom="@+id/weightTextBox"
android:layout_alignLeft="@+id/bmiButton"
android:layout_alignStart="@+id/bmiButton" />
</RelativeLayout>

最佳答案

您的weightheight 变量是int。那是你的问题。将它们更改为 floatdouble

目前,您正在进行整数除法,这导致结果被截断如下:1/2 给出 0

关于java - (Android)试图理解为什么我的方法不会在 TextView 中计算/显示正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com