[Android]計算BMI值範例

介面說明

手機畫面中有

身高 - 輸入框

體重 - 輸入框

送出 - 按鈕

結果 - 顯示文字及圖片

診斷 - 文字

介面BMI_main.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: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=".MainActivity">
	
	<TextView android:id="@+id/tv1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentLeft="true"
		android:layout_alignParentTop="true"
		android:text="身高:"
		android:textAppearance="?android:attr/textAppearanceMedium"
		/>
	<TextView
		android:id="@+id/tv2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignLeft="@+id/tv1"
		android:layout_below="@+id/tv1"
		android:layout_marginTop="15dp"
		android:text="體重:"
		android:textAppearance="?android:attr/textAppearanceMedium"
		/>
 
	<EditText
		android:id="@+id/et1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignTop="@+id/tv1"
		android:layout_marginLeft="15dp"
		android:layout_toRightOf="@+id/tv1"
		android:ems="10"/>
 
	<EditText
		android:id="@+id/et2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignLeft="@+id/et1"
		android:layout_below="@+id/et1"
		android:ems="10"
		/>
 
	<Button
		android:id="@+id/button1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignRight="@+id/et2"
		android:layout_below="@+id/et2"
		android:layout_marginTop="21dp"
		android:text="送出" />
 
	<TextView
		android:id="@+id/tv3"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_alignRight="@+id/button1"
		android:layout_below="@+id/button1"
		android:text="結果:"
		android:textAppearance="?android:attr/textAppearanceMedium"
		/>
 
	<TextView
		android:id="@+id/tv4"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_alignRight="@+id/button1"
		android:layout_below="@+id/tv3"
		android:layout_marginTop="10dp"
		android:text="診斷:"
		android:textAppearance="?android:attr/textAppearanceMedium"
		/>
</RelativeLayout>

撰寫BMI值功能

import java.text.NumberFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends Activity {
        EditText h;                //宣告全域變數
        EditText w;                //宣告全域變數
        @Override 
        protected void onCreate(Bundle savedInstanceState) {
 
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                h = (EditText)findViewById(R.id.et1);                            // 取得身高物件
                w = (EditText)findViewById(R.id.et2);                           // 取得體重物件
                Button submit = (Button)findViewById(R.id.button1); // 取得按鈕物件
 
                // 按下按鈕 觸發事件
                submit.setOnClickListener(new Button.OnClickListener() {
                        public void onClick(View arg0) {
                                //判斷條件 身高 跟 體重 都有輸入值才執行
                                if ( !("".equals(h.getText().toString())
                                     || "".equals(w.getText().toString())) )
                                {
                                        float fh = Float.parseFloat(h.getEditableText().toString());      // 取得 身高輸入值
                                        float fw = Float.parseFloat(w.getEditableText().toString());     // 取得 體重輸入值
                                        float fresult;                                     // BMI值 計算結果
                                        TextView result = (TextView)findViewById(R.id.tv3);// 取得 顯示結果 物件
                                        fh = fh/100; // 計算BMI
                                        fh = fh*fh;  // 計算BMI
 
                                        NumberFormat nf = NumberFormat.getInstance();   // 數字格式
                                        nf.setMaximumFractionDigits(2);                 // 限制小數第二位
                                        fresult = fw/fh;                                // 計算BMI
                                        result.setText(nf.format(fw/fh) +"");           // 顯示BMI計算結果
                                        TextView dia = (TextView)findViewById(R.id.tv4);// 取得 顯示診斷 物件
 
                                        // 診斷結果 顯示
                                        if (fresult<18.5)
                                                dia.setText("體重過輕");
                                        else if (18.5 <= fresult && fresult< 24)
                                                dia.setText("正常範圍");
                                        else if (24 <=fresult && fresult < 27)
                                                dia.setText("過    重");
                                        else if (27 <=fresult && fresult < 30)
                                                dia.setText("輕度肥胖");
                                        else if (30 <= fresult && fresult < 35)
                                                dia.setText("中度肥胖");
                                        else if (fresult >= 35)
                                                dia.setText("重度肥胖        ");
                                }
                        }
                });
        }
        @Override 
        public boolean onCreateOptionsMenu(Menu menu) {
 
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }
}


0 Reply   |  Until 2月前 | 81 View




熱門評論


LoginCan Publish Content