天天看点

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),在此函数内输入具体操作。