标准体重计算器

一 实验结果图


二 关键代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MawActivity" >

    <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" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

    <EditText
        android:id="@+id/etx"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn"
        android:layout_alignLeft="@+id/male"
        android:layout_marginBottom="37dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etx"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="57dp"
        android:text="@string/count" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="53dp"
        android:text="@string/sex" />

    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="女" />

    <RadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/female"
        android:text="男" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="26px"
        android:layout_alignLeft="@+id/etx"
        android:layout_centerVertical="true"
        android:text="@string/heigh" />

</RelativeLayout>

package com.example.manandwoman;

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; 


public class MawActivity extends Activity {  
    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.activity_maw);  
        creadView();  
        sexChoose();  
        setListener();  
   }  
    
    private void setListener() {  
        countButton.setOnClickListener(countListner);  
    }  
 
    private OnClickListener countListner = new OnClickListener() {  
          
        @Override 
        public void onClick(View v) {  
            Toast.makeText(MawActivity.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(){   
        countButton=(Button)findViewById(R.id.btn);  
        heighText=(EditText)findViewById(R.id.etx);  
        maleBtn=(RadioButton)findViewById(R.id.male);  
        femaleBtn=(RadioButton)findViewById(R.id.female);      
    }  
  
    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;  
       }  
   } 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Manandwoman</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="hello">标准体重计算器</string>
    <string name="sex">选择性别</string>
    <string name="heigh">输入身高</string>
    <string name="count">计算</string>

</resources>

三 遇到的问题

R.id.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值