2011年8月16日 星期二

BMI計算 練習

res\layout\main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/linearLayout1"
    android:weightSum="1"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:text="" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
   
<TextView
        android:text="@string/bmi_height"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:bufferType="normal"
        android:layout_height="wrap_content">
    </TextView>
    <EditText
        android:id="@+id/editText1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <requestFocus></requestFocus>
    </EditText>
    <TextView
        android:text="@string/bmi_wid"
        android:id="@+id/textView2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"></TextView>
    <EditText
        android:id="@+id/editText2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"></EditText>
    <Button
        android:text="@string/bmi_data"
        android:id="@+id/button1"
        android:layout_width="117dp"
        android:layout_height="wrap_content"></Button>
    <TextView android:text="" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</LinearLayout>

res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, FestTest!</string>
    <string name="app_name">Hello, World</string>
    <string name="bmi_height">身高(CM):</string>
    <string name="bmi_wid">體重(KG):</string>
    <string name="bmi_data">計算BMI</string>
    <string name="bmi_heavy">過重</string>
    <string name="bmi_light">太輕</string>
    <string name="bmi_average">標準</string>
   
</resources>

src\com\android\test\Test.java
package com.android.test;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.*;

public class Test 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.button1);
        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.editText1);
            EditText fieldweight = (EditText) findViewById(R.id.editText2);

            double height = Double
                    .parseDouble(fieldheight.getText().toString());
            double weight = Double
                    .parseDouble(fieldweight.getText().toString());
            double BMI = weight / ((height / 100) * (height / 100));

            TextView result = (TextView) findViewById(R.id.textView3);
            result.setText("Your BMI is " + nf.format(BMI));
           
            TextView fieldsuggest = (TextView)findViewById(R.id.textView4);
            if(BMI>25){
                fieldsuggest.setText(R.string.bmi_heavy);
            }else if(BMI<20){
                fieldsuggest.setText(R.string.bmi_light);
            }else{
                fieldsuggest.setText(R.string.bmi_average);
            }

        };

    };
}

沒有留言 :

張貼留言