Android開發(fā):標(biāo)準(zhǔn)體重計(jì)算器應(yīng)用的開發(fā)實(shí)例
應(yīng)用的操作和原理
目標(biāo)Android應(yīng)用的操作過程是這樣的:選擇你的性別,然后輸入你的身高,點(diǎn)查看計(jì)算結(jié)果的按鈕就在Toast中顯示你的標(biāo)準(zhǔn)體重。力求操作簡單,結(jié)果顯示清楚。
標(biāo)準(zhǔn)體重的計(jì)算公式:
男性:(身高cm-80)×70﹪=標(biāo)準(zhǔn)體重
女性:(身高cm-70)×60﹪=標(biāo)準(zhǔn)體重
應(yīng)用的源碼
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 * 該程序的功能是用戶選擇自己的性別和輸入自己的身高,然后點(diǎn)擊按鈕,就能在Toast顯示出自己的標(biāo)準(zhǔn)體重
- */
- 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);
- //調(diào)用創(chuàng)建視圖的函數(shù)
- creadView();
- //調(diào)用性別選擇的函數(shù)
- sexChoose();
- //調(diào)用Button注冊監(jiān)聽器的函數(shù)
- setListener();
- }
- //響應(yīng)Button事件的函數(shù)
- 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你的標(biāo)準(zhǔn)體重為"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
- .show();
- }
- };
- //性別選擇的函數(shù)
- private String sexChoose(){
- if (maleBtn.isChecked()) {
- sex = "男性";
- }
- else if(femaleBtn.isChecked()){
- sex = "女性";
- }
- return sex;
- }
- //創(chuàng)建視圖的函數(shù)
- 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);
- }
- //標(biāo)準(zhǔn)體重格式化輸出的函數(shù)
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String str = formatter.format(num);
- return str;
- }
- //得到標(biāo)準(zhǔn)體重的函數(shù)
- 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與體重計(jì)算器應(yīng)用效果圖展示
#p#
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>
應(yīng)用效果圖
大家可以根據(jù)其他復(fù)雜的標(biāo)準(zhǔn)體重計(jì)算器繼續(xù)完善此應(yīng)用,使其成為一個(gè)可用的、美觀的Android應(yīng)用。