天天看點

android圖形化程式設計_計算BMI值

第一次在android平台上做圖形化程式設計,特做筆記,以便加深印象及後續查閱。此程式來自《深入淺出android》

1、              建立工程,file—new—android project,輸入工程名helloandroid01,package名稱為com.android.hello

2、              編輯視圖,打開helloandroid01—res—layout—main.xml,輸入以下代碼

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <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="@string/bmi_btn"    />

    <TextView

    android:id="@+id/result"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/bmi_result" />

    <TextView

    android:id="@+id/suggest"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="" />

</LinearLayout>

3、              增加字元串定義,打開helloandroid01—res—values—string.xml,輸入以下代碼

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, Helloandroid01Activity!</string>

    <string name="app_name">Helloandroid01</string>

    <string name="height">身高(cm)</string>

    <string name="weight">體重(kg)</string>

    <string name="bmi_btn">計算你的bmi值</string>

    <string name="bmi_result">你的BMI值是</string>

</resources>

4、              增加建議,建立xml文檔,位置在helloandroid01—res—values目錄下,以下為代碼

<?xml version="1.0" encoding="UTF-8"?>

<resources>

    <string name="advice_light">你太輕了!</string>

    <string name="advice_average">體形很棒哦!</string>

    <string name="advice_heavy">你太肥了!</string>

</resources>

5、              在主文檔helloandroid01Activity.java中添加相關代碼

package com.android.hello;

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.EditText;

import android.widget.TextView;

public class Helloandroid01Activity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //Listen for button clicks

        Button button = (Button)findViewById(R.id.submit);

        button.setOnClickListener(calcBMI);

    }//end onCreate

    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("你的BMI值是"+nf.format(BMI));

        //Give health advise

        TextView fieldsugest = (TextView)findViewById(R.id.suggest);

        if(BMI>25)

        {

            fieldsugest.setText(R.string.advice_heavy);

        }

        else if(BMI<20)

        {

            fieldsugest.setText(R.string.advice_light);

        }

        else

        {

            fieldsugest.setText(R.string.advice_average);

        }

    }

    };

}

6、              點選run—run,運作結果如下圖所示

android圖形化程式設計_計算BMI值

收獲:

1.      了解了android.widget.Button、EditText、TextView等類

2.      熟悉了類與界面元素之間的對應方法,可以用findViewById方法,例如EditText fieldheight = (EditText)findViewById(R.id.height);

3.      向界面傳輸字元串result.setText("你的BMI值是"+nf.format(BMI));

4.      了解了變量定義的方法,在main.xml中可以定義,例如android:id="@+id/submit"

5.      了解了變量的指派方法,可以在string.xml或者其他xml檔案中指派,指派格式為<string name="height">身高(cm)</string>

6.      了解了如何建立其他xml檔案的方法

7.      了解了變量使用的方法,例如android:text="@string/weight" />

8.      了解了按鈕響應函數的方法,先定義button.setOnClickListener(calcBMI);然後建立對象private OnClickListener calcBMI = new OnClickListener();在對象裡定義函數public void onClick(View v),在此函數内輸入具體操作。