标准体重计算器

一 实验结果图


二 关键代码

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MawActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/txt"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:gravity="center"  
  16.         android:text="@string/hello"  
  17.         android:textSize="16px" />  
  18.   
  19.     <RadioGroup  
  20.         android:id="@+id/radioGroup1"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:orientation="horizontal" >  
  24.     </RadioGroup>  
  25.   
  26.     <EditText  
  27.         android:id="@+id/etx"  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_above="@+id/btn"  
  31.         android:layout_alignLeft="@+id/male"  
  32.         android:layout_marginBottom="37dp"  
  33.         android:ems="10" >  
  34.   
  35.         <requestFocus />  
  36.     </EditText>  
  37.   
  38.     <Button  
  39.         android:id="@+id/btn"  
  40.         android:layout_width="fill_parent"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignLeft="@+id/etx"  
  43.         android:layout_alignParentBottom="true"  
  44.         android:layout_marginBottom="57dp"  
  45.         android:text="@string/count" />  
  46.   
  47.     <TextView  
  48.         android:id="@+id/textView1"  
  49.         android:layout_width="fill_parent"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_alignLeft="@+id/radioGroup1"  
  52.         android:layout_below="@+id/radioGroup1"  
  53.         android:layout_marginTop="53dp"  
  54.         android:text="@string/sex" />  
  55.   
  56.     <RadioButton  
  57.         android:id="@+id/female"  
  58.         android:layout_width="wrap_content"  
  59.         android:layout_height="wrap_content"  
  60.         android:layout_below="@+id/textView1"  
  61.         android:layout_centerHorizontal="true"  
  62.         android:layout_marginTop="34dp"  
  63.         android:text="女" />  
  64.   
  65.     <RadioButton  
  66.         android:id="@+id/male"  
  67.         android:layout_width="wrap_content"  
  68.         android:layout_height="wrap_content"  
  69.         android:layout_alignLeft="@+id/textView1"  
  70.         android:layout_alignTop="@+id/female"  
  71.         android:text="男" />  
  72.   
  73.     <TextView  
  74.         android:layout_width="fill_parent"  
  75.         android:layout_height="26px"  
  76.         android:layout_alignLeft="@+id/etx"  
  77.         android:layout_centerVertical="true"  
  78.         android:text="@string/heigh" />  
  79.   
  80. </RelativeLayout>  

[java]  view plain  copy
  1. package com.example.manandwoman;  
  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.   
  15. public class MawActivity extends Activity {    
  16.     private Button countButton;    
  17.     private EditText heighText;    
  18.     private RadioButton maleBtn, femaleBtn;     
  19.     String sex = "";    
  20.     double height;    
  21.     @Override   
  22.     public void onCreate(Bundle savedInstanceState) {    
  23.         super.onCreate(savedInstanceState);    
  24.         setContentView(R.layout.activity_maw);    
  25.         creadView();    
  26.         sexChoose();    
  27.         setListener();    
  28.    }    
  29.       
  30.     private void setListener() {    
  31.         countButton.setOnClickListener(countListner);    
  32.     }    
  33.    
  34.     private OnClickListener countListner = new OnClickListener() {    
  35.             
  36.         @Override   
  37.         public void onClick(View v) {    
  38.             Toast.makeText(MawActivity.this"你是一位"+sexChoose()+"\n"   
  39.                            +"你的身高为"+Double.parseDouble(heighText.getText().toString())+"cm"   
  40.                            +"\n你的标准体重为"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)    
  41.                            .show();    
  42.         }    
  43.     };    
  44.        
  45.     private String sexChoose(){         
  46.         if (maleBtn.isChecked()) {    
  47.             sex = "男性";    
  48.         }     
  49.         else if(femaleBtn.isChecked()){    
  50.             sex = "女性";    
  51.         }    
  52.         return sex;         
  53.     }    
  54.     public void creadView(){     
  55.         countButton=(Button)findViewById(R.id.btn);    
  56.         heighText=(EditText)findViewById(R.id.etx);    
  57.         maleBtn=(RadioButton)findViewById(R.id.male);    
  58.         femaleBtn=(RadioButton)findViewById(R.id.female);        
  59.     }    
  60.     
  61.     private String format(double num) {    
  62.         NumberFormat formatter = new DecimalFormat("0.00");    
  63.         String str = formatter.format(num);    
  64.         return str;    
  65.         }    
  66.         
  67.     private String getWeight(String sex, double height) {    
  68.         height = Double.parseDouble(heighText.getText().toString());    
  69.         String weight = "";    
  70.         if (sex.equals("男性")) {    
  71.               weight =format((height - 80) * 0.7);    
  72.         }     
  73.         else {    
  74.               weight = format((height - 70) * 0.6);    
  75.         }    
  76.         return weight;    
  77.        }    
  78.    }   

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">Manandwoman</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="hello_world">Hello world!</string>  
  7.     <string name="hello">标准体重计算器</string>  
  8.     <string name="sex">选择性别</string>  
  9.     <string name="heigh">输入身高</string>  
  10.     <string name="count">计算</string>  
  11.   
  12. </resources>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款由Google开发的集成开发环境(IDE),用于开发Android应用程序。标准体重计算器是一个简单的应用程序,用于根据身高和性别计算一个人的标准体重。 在Android Studio中创建一个标准体重计算器应用程序的步骤如下: 1. 创建一个新的Android项目,并选择合适的项目名称和位置。 2. 在布局文件中设计应用程序的用户界面,可以使用TextView、EditText和Button等控件来接收用户输入和显示计算结果。 3. 在Java代码中编写逻辑来处理用户输入和计算标准体重。可以使用公式:男性标准体重 = (身高 - 100)* 0.9,女性标准体重 = (身高 - 100)* 0.85。 4. 将计算结果显示在应用程序界面上。 以下是一个简单的示例代码: ```java public class MainActivity extends AppCompatActivity { private EditText etHeight; private Button btnCalculate; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etHeight = findViewById(R.id.et_height); btnCalculate = findViewById(R.id.btn_calculate); tvResult = findViewById(R.id.tv_result); btnCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String heightStr = etHeight.getText().toString(); if (!TextUtils.isEmpty(heightStr)) { double height = Double.parseDouble(heightStr); double weight; if (/* 判断性别 */) { weight = (height - 100) * 0.9; } else { weight = (height - 100) * 0.85; } tvResult.setText("标准体重:" + weight + "kg"); } } }); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值