計算標准體重的方法:
男性:(身高cm-80)×70﹪=標准體重 女性:(身高cm-70)×60﹪=標准體重
背景圖片下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/1月/29日/Android開發應用實例:計算標准體重的實例(簡單版)/
BMIActivity.java
- package com.lingdududu.bmi;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- 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.RadioButton;
- import android.widget.Toast;
- /*
- * @author lingdududu * 該程序的功能是用戶選擇自己的性別和輸入自己的身高,然後點擊按鈕,就能在Toast顯示出自己的標准體重
- */
- public class BMIActivity extends Activity {
- /** Called when the activity is first created. */
- private Button countButton;
- private EditText heighText;
- private RadioButton maleBtn, femaleBtn;
- String sex = "";
- double height;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //調用創建視圖的函數
- creadView();
- //調用性別選擇的函數
- sexChoose();
- //調用Button注冊監聽器的函數
- setListener();
- }
- //響應Button事件的函數
- private void setListener() {
- countButton.setOnClickListener(countListner);
- }
- private OnClickListener countListner = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(BMIActivity.this, "你是一位"+sexChoose()+"\n"
- +"你的身高為"+Double.parseDouble(heighText.getText().toString())+"cm"
- +"\n你的標准體重為"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
- .show();
- }
- };
- //性別選擇的函數
- private String sexChoose(){
- if (maleBtn.isChecked()) {
- sex = "男性";
- }
- else if(femaleBtn.isChecked()){
- sex = "女性";
- }
- return sex;
- }
- //創建視圖的函數
- public void creadView(){
- //txt=(TextView)findViewById(R.id.txt);
- countButton=(Button)findViewById(R.id.btn);
- heighText=(EditText)findViewById(R.id.etx);
- maleBtn=(RadioButton)findViewById(R.id.male);
- femaleBtn=(RadioButton)findViewById(R.id.female);
- //txt.setBackgroundResource(R.drawable.bg);
- }
- //標准體重格式化輸出的函數
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String str = formatter.format(num);
- return str;
- }
- //得到標准體重的函數
- private String getWeight(String sex, double height) {
- height = Double.parseDouble(heighText.getText().toString());
- String weight = "";
- if (sex.equals("男性")) {
- weight =format((height - 80) * 0.7);
- }
- else {
- weight = format((height - 70) * 0.6);
- }
- return weight;
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/pic"
- >
- <TextView
- android:id="@+id/txt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="@string/hello"
- android:textSize="16px"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/sex"
- />
- <RadioGroup
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <RadioButton
- android:id="@+id/male"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男"
- />
- <RadioButton
- android:id="@+id/female"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女"
- />
- </RadioGroup>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="26px"
- android:text="@string/heigh"
- />
- <EditText
- android:id="@+id/etx"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/count"
- />
- </LinearLayout>
效果圖:
650) this.width=650;">