BMI计算器应用程序

时间:2014-02-28 04:55:19

标签: java android

我是android的新手。我做了一个BMI计算器。它工作正常。但我想放置一些图形或动画。例如,如果BMI结果为UNDERWEIGHT,则会弹出UNDERWEIGHT图片的图像。请帮助。

这是我的activity_sample.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SampleActivity"
 android:background="@drawable/backg" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/weightLabel"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/weightText"
    android:layout_width="70dp"
    android:layout_height="50dp"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="14dp"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="number"
    android:maxLength="3"
    android:text="@string/weightText" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="45dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="@string/heightLabel"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/heightText"
    android:layout_width="70dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/weightText"
    android:layout_alignBottom="@+id/weightText"
    android:layout_alignRight="@+id/textView2"
    android:ems="10"
    android:inputType="number"
    android:maxLength="2"
    android:text="@string/heightText" />

<TextView
    android:id="@+id/gender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/weightText"
    android:layout_marginTop="16dp"
    android:text="@string/genderLabel"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/calculateButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/heightText"
    android:layout_alignTop="@+id/gender"
     android:onClick="calculateClickHandler"
    android:text="@string/calculateButton" />

<RadioGroup
    android:id="@+id/rdGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/gender"
    android:layout_toRightOf="@+id/gender" >

    <RadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/maleLabel" />

    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/femaleLabel" />
</RadioGroup>

<TextView
    android:id="@+id/yourBMI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/gender"
    android:layout_centerVertical="true"
    android:textStyle="bold"
    android:text="@string/yourBMI" />

<TextView
    android:id="@+id/resultLabel"
    android:layout_width="wrap_content"
    android:layout_height="55dp"
    android:layout_alignBaseline="@+id/yourBMI"
    android:layout_alignBottom="@+id/yourBMI"
    android:layout_alignRight="@+id/calculateButton"
    android:layout_toRightOf="@+id/weightText"
    android:text="@string/emptyString"
    android:textColor="#000000"
    android:textStyle="bold" />

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/frameLayout"
    android:background="#b2ff7c">

这是我的SampleActivity.java

package com.sample.sample;



import android.os.Bundle;
import android.app.Activity;
import android.view.View;

import android.view.Menu;


import android.widget.EditText;
import android.widget.TextView;

public class SampleActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);


}

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

public void calculateClickHandler(View view) {


    if (view.getId() == R.id.calculateButton) {

    EditText weightText = (EditText)   findViewById(R.id.weightText);
    EditText heightText = (EditText)findViewById(R.id.heightText);
    TextView resultText = (TextView)findViewById(R.id.resultLabel);


    int weight = (int) Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());

    int bmiValue = calculateBMI(weight, height);

    String bmiInterpretation = interpretBMI(bmiValue);

    resultText.setText(bmiValue + "and you are" + bmiInterpretation); }
    }

    private int calculateBMI (int weight, int height) {

    return (int)  weight * 703 / (height * height) ;

    }
    private String interpretBMI(int bmiValue) {

    if (bmiValue < 18.5) {
        return "Underweight";
    } else if (bmiValue < 25){
        return "Normal";
    } else if (bmiValue < 30) {
        return "Overweight";
    } else {
        return "Obese";
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

我找不到确切的方法,但这里的想法是一样的。

http://developer.android.com/training/animation/zoom.html

使用此页面中的想法,您可以简单地在某人点击计算或不计算时弹出图像。祝你好运!