27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

6 修 改 BMIActivity.java 内 容<br />

package zyf.Ex10_UI;<br />

/* import 相 关 class */<br />

import java.text.DecimalFormat;<br />

import java.text.NumberFormat;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.widget.TextView;<br />

public class BMIActivity extends Activity {<br />

/** Called when the activity is first created. */<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

}<br />

/* 加 载 main.xml Layout */<br />

setContentView(R.layout.mylayout);<br />

/* 取 得 Intent 中 的 Bundle 对 象 */<br />

Bundle bunde = this.getIntent().getExtras();<br />

/* 取 得 Bundle 对 象 中 的 数 据 */<br />

String sex = bunde.getString("sex");<br />

double height = bunde.getDouble("height");<br />

/* 判 断 性 别 */<br />

String sexText = "";<br />

if (sex.equals("M")) {<br />

sexText = " 男 性 ";<br />

} else {<br />

}<br />

sexText = " 女 性 ";<br />

/* 取 得 标 准 体 重 */<br />

String weight = this.getWeight(sex, height);<br />

/* 设 置 输 出 文 字 */<br />

TextView tv1 = (TextView) findViewById(R.id.text1);<br />

tv1.setText(" 你 是 一 位 " + sexText + "\n 你 的 身 高 是 " + height +<br />

" 厘 米 \n 你 的 标 准 体 重 是 "+ weight + " 公 斤 ");<br />

/* 四 舍 五 入 的 method */<br />

private String format(double<br />

num) {<br />

NumberFormat formatter = new DecimalFormat("0.00");<br />

String s = formatter.format(num);<br />

return s;<br />

}<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!