Android开发:标准体重计算器应用的开发实例

移动开发 Android
本文介绍一个简易的标准体重计算器Android应用的开发实例。此功能在以前的手机中我们也经常看到。

应用的操作和原理

目标Android应用的操作过程是这样的:选择你的性别,然后输入你的身高,点查看计算结果的按钮就在Toast中显示你的标准体重。力求操作简单,结果显示清楚。

标准体重的计算公式:

男性:(身高cm-80)×70﹪=标准体重

女性:(身高cm-70)×60﹪=标准体重

应用的源码

BMIActivity.java:

  1. package com.lingdududu.bmi;    
  2. import java.text.DecimalFormat;    
  3. import java.text.NumberFormat;    
  4. import android.app.Activity;    
  5. import android.os.Bundle;    
  6. import android.view.View;    
  7. import android.view.View.OnClickListener;    
  8. import android.widget.Button;     
  9. import android.widget.EditText;    
  10. import android.widget.RadioButton;    
  11. import android.widget.Toast;      
  12. /*   
  13. * @author lingdududu * 该程序的功能是用户选择自己的性别和输入自己的身高,然后点击按钮,就能在Toast显示出自己的标准体重   
  14. */   
  15. public class BMIActivity extends Activity {    
  16. /** Called when the activity is first created. */   
  17.     private Button countButton;      
  18.     private EditText heighText;      
  19.     private RadioButton maleBtn, femaleBtn;       
  20.     String sex = "";      
  21.     double height;      
  22.     @Override     
  23.     public void onCreate(Bundle savedInstanceState) {      
  24.         super.onCreate(savedInstanceState);      
  25.         setContentView(R.layout.main);      
  26.         //调用创建视图的函数      
  27.         creadView();      
  28.         //调用性别选择的函数      
  29.         sexChoose();      
  30.         //调用Button注册监听器的函数      
  31.         setListener();      
  32.    }      
  33.     //响应Button事件的函数      
  34.     private void setListener() {      
  35.         countButton.setOnClickListener(countListner);      
  36.     }      
  37.     private OnClickListener countListner = new OnClickListener() {      
  38.         @Override     
  39.         public void onClick(View v) {      
  40.             // TODO Auto-generated method stub      
  41.             Toast.makeText(BMIActivity.this"你是一位"+sexChoose()+"\n"     
  42.                            +"你的身高为"+Double.parseDouble(heighText.getText().toString())+"cm"     
  43.                            +"\n你的标准体重为"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)      
  44.                            .show();      
  45.         }      
  46.     };      
  47.     //性别选择的函数      
  48.     private String sexChoose(){           
  49.         if (maleBtn.isChecked()) {      
  50.             sex = "男性";      
  51.         }       
  52.         else if(femaleBtn.isChecked()){      
  53.             sex = "女性";      
  54.         }      
  55.         return sex;           
  56.     }      
  57.     //创建视图的函数      
  58.     public void creadView(){      
  59.         //txt=(TextView)findViewById(R.id.txt);      
  60.         countButton=(Button)findViewById(R.id.btn);      
  61.         heighText=(EditText)findViewById(R.id.etx);      
  62.         maleBtn=(RadioButton)findViewById(R.id.male);      
  63.         femaleBtn=(RadioButton)findViewById(R.id.female);         
  64.         //txt.setBackgroundResource(R.drawable.bg);      
  65.     }      
  66.     //标准体重格式化输出的函数      
  67.     private String format(double num) {   
  68.         NumberFormat formatter = new DecimalFormat("0.00");      
  69.         String str = formatter.format(num);      
  70.         return str;      
  71.         }      
  72.     //得到标准体重的函数      
  73.     private String getWeight(String sex, double height) {      
  74.         height = Double.parseDouble(heighText.getText().toString());      
  75.         String weight = "";      
  76.         if (sex.equals("男性")) {      
  77.               weight =format((height - 80) * 0.7);      
  78.         }       
  79.         else {      
  80.               weight = format((height - 70) * 0.6);      
  81.         }      
  82.         return weight;      
  83.        }      
  84.    }     

别走开,下页为您带来main.xml与体重计算器应用效果图展示

#p#

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:orientation="vertical"     
  4.     android:layout_width="fill_parent"     
  5.     android:layout_height="fill_parent"     
  6.     android:background="@drawable/pic"     
  7.     >     
  8.     <TextView        
  9.         android:id="@+id/txt"     
  10.         android:layout_width="fill_parent"       
  11.         android:layout_height="wrap_content"       
  12.         android:gravity="center"       
  13.         android:text="@string/hello"     
  14.         android:textSize="16px"       
  15.         />     
  16.    <TextView        
  17.         android:layout_width="fill_parent"       
  18.         android:layout_height="wrap_content"       
  19.         android:text="@string/sex"         
  20.         />     
  21.    <RadioGroup       
  22.       android:layout_width="fill_parent"       
  23.       android:layout_height="wrap_content"       
  24.       android:orientation="horizontal"     
  25.       >        
  26.       <RadioButton       
  27.            android:id="@+id/male"     
  28.            android:layout_width="wrap_content"       
  29.            android:layout_height="wrap_content"       
  30.            android:text="男"       
  31.            />       
  32.       <RadioButton       
  33.            android:id="@+id/female"     
  34.            android:layout_width="wrap_content"       
  35.            android:layout_height="wrap_content"     
  36.            android:text="女"       
  37.            />       
  38.    </RadioGroup>       
  39.    <TextView        
  40.         android:layout_width="fill_parent"       
  41.         android:layout_height="26px"     
  42.         android:text="@string/heigh"     
  43.         />     
  44.    <EditText     
  45.         android:id="@+id/etx"     
  46.         android:layout_width="fill_parent"       
  47.         android:layout_height="wrap_content"       
  48.         />     
  49.    <Button     
  50.          android:id="@+id/btn"     
  51.          android:layout_width="fill_parent"       
  52.          android:layout_height="wrap_content"     
  53.          android:text="@string/count"     
  54.          />     
  55. </LinearLayout>    

应用效果图

大家可以根据其他复杂的标准体重计算器继续完善此应用,使其成为一个可用的、美观的Android应用。

责任编辑:闫佳明 来源: jizhuomi
相关推荐

2022-08-09 16:01:24

应用开发鸿蒙

2016-12-12 13:41:37

iOS简易加法开发

2009-04-02 15:58:12

AndroidEclipseSqlite

2011-09-08 13:11:07

Android Wid实例

2017-09-05 16:43:47

Electron桌面计算器

2009-12-16 10:41:47

Android日程表

2013-05-20 17:51:47

Android游戏开发SurfaceView

2011-09-26 10:46:32

Android云计算开发

2013-05-20 15:42:22

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-09-16 14:13:15

Windows7计算器

2013-05-20 17:13:17

Android游戏开发CanvasPaint

2013-02-20 15:29:00

JSONAndroid开发

2011-09-07 17:54:40

Android Wid开发

2011-07-26 11:08:23

iOS 录像 录音

2017-07-18 14:28:04

HTMLCSSJS

2019-06-26 08:24:07

Windows 功能系统

2022-02-15 14:06:36

OpenHarmon操作系统鸿蒙

2012-03-16 20:33:20

iPhone

2011-04-12 08:40:23

IMFAndroid
点赞
收藏

51CTO技术栈公众号