博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发应用实例:计算标准体重的实例(简单版)
阅读量:7030 次
发布时间:2019-06-28

本文共 3702 字,大约阅读时间需要 12 分钟。

 下面是一个简单的计算标准体重的实例,选择自己的性别,再输入自己的身高,点击Button就能在Toast显示自己的标准体重,看看自己的体重有没有符合标准哦。

计算标准体重的方法:

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

BMIActivity.java

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

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> 

效果图:

 

 

本文转自 lingdududu 51CTO博客,原文链接: 

http://blog.51cto.com/liangruijun/700077

转载地址:http://thgxl.baihongyu.com/

你可能感兴趣的文章
技术寡头争霸传之:控制开源工具,就控制了整个生态
查看>>
微软把UWP定位成业务线应用程序开发平台
查看>>
2018腾讯云+未来峰会互联网专场:腾讯云智能物联解决方案亮相
查看>>
Python数据可视化的10种技能
查看>>
关于有效的性能调优的一些建议
查看>>
微软发起Java on Azure调查,呼吁Java社区积极参与
查看>>
搭建svn仓库
查看>>
JavaScript arguments 对象详解
查看>>
[elixir! #0002] [译] 在Phoenix中实现动态表单 by José Valim
查看>>
【windows docker & centos 6 .7搭建】
查看>>
Firefox插件开发:夜间模式
查看>>
这是我第一次遇到判断ios系统版本的问题
查看>>
漏洞战争: 软件漏洞发展趋势
查看>>
数人云CTO解读Docker 1.12和金融业容器化
查看>>
PyMongo 连接问题
查看>>
切图崽的自我修养-合理组织CSS结构
查看>>
【编码】Font Size-微软实习笔试编程题(一)-2016.04.06
查看>>
PHP|PHP引用的简单解释
查看>>
gulp之JS、CSS、HTML、图片压缩以及版本更新
查看>>
实录分享|一篇文章看CNTV的容器化探索和平台搭建
查看>>