bmi.java file content:
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View;
public class bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI=new OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf=new DecimalFormat("0.00");
EditText fieldheight=(EditText)findViewById(R.id.height);
EditText fieldweight=(EditText)findViewById(R.id.weight);
double height=Double.parseDouble(fieldheight.getText().toString())/100;
double weight=Double.parseDouble(fieldweight.getText().toString());
double bmi=weight/(height*height);
TextView result =(TextView)findViewById(R.id.result);
result.setText("Your bmi is "+nf.format(bmi));
TextView suggest =(TextView)findViewById(R.id.suggest);
if(bmi>25)
suggest.setText(R.string.advice_heavy);
else if(bmi<20)
suggest.setText(R.string.advice_light);
else suggest.setText(R.string.advice_average);
}
};
}
main.xml file content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/height"
/>
<EditText android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weight"
/>
<EditText android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算 BIM 值"
/>
<TextView android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
string.xml content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, bmi!</string>
<string name="app_name">bmi</string>
<string name="height">身高(CM)</string>
<string name="weight">体重(Kg)</string>
</resources>
advice.xml content:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="advice_light">you should eat more!</string>
<string name="advice_average">you have good healthy</string>
<string name="advice_heavy">you should have a diet</string>
</resources>
这是一个简单的BMI计算器应用程序源代码,使用Java编写并针对Android平台。该应用允许用户输入身高和体重,然后计算并显示BMI指数及健康建议。
2104

被折叠的 条评论
为什么被折叠?



