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

标准体重的计算公式:
男性:(身高cm-80)×70﹪=标准体重
女性:(身高cm-70)×60﹪=标准体重
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与体重计算器应用效果图展示
#p#
main.xml:
- android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:background="@drawable/pic"
 - >
 - android:id="@+id/txt"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:gravity="center"
 - android:text="@string/hello"
 - android:textSize="16px"
 - />
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="@string/sex"
 - />
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal"
 - >
 - android:id="@+id/male"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="男"
 - />
 - android:id="@+id/female"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="女"
 - />
 - android:layout_width="fill_parent"
 - android:layout_height="26px"
 - android:text="@string/heigh"
 - />
 - android:id="@+id/etx"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - />
 
应用效果图
大家可以根据其他复杂的标准体重计算器继续完善此应用,使其成为一个可用的、美观的Android应用。
                网站题目:Android开发:标准体重计算器应用的开发实例
                
                当前网址:http://www.csdahua.cn/qtweb/news5/366505.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网