标准体重计算器

标准体重计算器

如果是男性,标准体重=(身高-80*0.7

如果是女性,标准体重=(身高-70*0.6


layout中设计两个布局文件:

activity_xiao.xml用来显示初始时的界面,填入信息后跳转到第二个界面显示标准体重

activity_second.xml 第二个界面,显示体重

src中两个activity:

Xiao.java中

package com.example.newx;



import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import android.app.Activity;
import android.content.Intent;


public class Xiao extends Activity {
    
	RadioGroup rg=null;
	String xingBie="男";//默认值为男
	Button button;
	EditText editT;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_xiao);
		//找到相应的组件
		rg=(RadioGroup)this.findViewById(R.id.group);
		button=(Button)this.findViewById(R.id.button1);
		editT=(EditText)this.findViewById(R.id.editText1);
                //对RadioGroup组件进行监听,从而确定按下的是男还是女
		rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				// TODO Auto-generated method stub
				switch(checkedId)
				{
				case R.id.nan:
					xingBie="男";break;
				case R.id.nv:
					xingBie="女";break;
					
				
				}
				
			}
		});//radioGroup判断结束
		//对计算按钮进行监听,并性别及身高的值传到下一个activity
	button.setOnClickListener(new OnClickListener()
	{
	
		public void onClick(View v)
		{
			String shenGao=editT.getText().toString();//取得身高的值
			Intent intent=new Intent();//利用intent进行跳转
			intent.setClass(Xiao.this, SecondActivity.class);
			Bundle bundle=new Bundle();//利用Bundle进行传值
			bundle.putString("xingBie", xingBie);//在Bundle中放入
			bundle.putString("shenGao", shenGao);
			intent.putExtras(bundle);
			startActivity(intent);
			
		}
	}
	);
	

	
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.xiao, menu);
		return true;
	}

}

SecondActivity.java中

		setContentView(R.layout.activity_second);
		tV=(TextView)this.findViewById(R.id.result);
		//b=(Button)this.findViewById(R.id.button1);
		Intent intent=getIntent();//获得Intent
		Bundle bundle=intent.getExtras();//取得Intent绑定的Bundle
		String xingBie=bundle.getString("xingBie");//取得性别和身高信息
		shenGao=bundle.getString("shenGao");
                    //根据性别进行相应的计算,并显示
		if(xingBie.equals("男"))
		{
			double tiZhong=((Integer.parseInt(shenGao))-80)*0.7;
			tV.setText("您是一名男性\n身高为:"+shenGao+"\n标准体重为:"+tiZhong);
		}
		else	
		{
			double tiZhong=((Integer.parseInt(shenGao))-70)*0.6;
			tV.setText("您是一名女性\n身高为:"+shenGao+"\n标准体重为:"+tiZhong);
		}
		
		
	
	}
	//第一种finish()
	public void fanHui(View view)
	{
	SecondActivity.this.finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.second, menu);
		return true;
	}

}



 

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、付费专栏及课程。

余额充值