标准体重计算器

题目要求:如果是男性,标准体重=(身高-80)*0.7

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

训练目标1.Intent的使用

                            2. 掌握如何在不同Activity之间通过Bundle传递数据

布局文件显示界面:actity_mian.xml

<LinearLayout 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:layout_centerHorizontal="true"
    android:orientation="vertical"
    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=".MainActivity" >

    <TextView
        android:id="@+id/compute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Compute" />

    <TextView
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Sex" />

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/M"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/m" />

        <RadioButton
            android:id="@+id/F"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="@string/f" />
    </RadioGroup>

    <TextView
        android:id="@+id/Height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/height" />

    <EditText
        android:id="@+id/ht"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" />

    <Button
        android:id="@+id/ca"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Ca" />

</LinearLayout>

布局文件actity_show.xml

<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=".ShowActivity" >

    <TextView
        android:id="@+id/showResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/showResult"
        android:text="回到上一页" />

</RelativeLayout>


 

MainActivity.java代码:

package com.example.compute;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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;

public class MainActivity extends Activity {
	private Button ca;
	private EditText heightText;

	private RadioGroup group;
	private String Sex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ca = (Button) this.findViewById(R.id.ca);
		heightText = (EditText) this.findViewById(R.id.ht);

		group = (RadioGroup) this.findViewById(R.id.group);

		// 绑定事件源和事件处理者
		ca.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (group.getCheckedRadioButtonId() == R.id.M) {
					Sex = "M";
				} else {
					Sex = "F";
				}

				Intent intent = new Intent(MainActivity.this,
						ShowActivity.class);
//Intent 实现页面跳转
				String height = heightText.getText().toString();
				Bundle bundle = new Bundle();
//Bundle用来绑定数据		bundle.putString("Sex", Sex);
				bundle.putString("ht", height);

				intent.putExtras(bundle);

				startActivity(intent);

				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.main, menu);
		return true;
	}

}

ShowActivity.java代码:


 

package com.example.compute;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.TextView;

public class ShowActivity extends Activity {
	private TextView showResult;
	private String Sex;
	private String str;
	private double weight;
//变量体重private Button button1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		showResult = (TextView) this.findViewById(R.id.showResult);
		button1 = (Button) this.findViewById(R.id.button1);

		Intent intent = getIntent();
//获取Intent
		Bundle bundle = intent.getExtras();
//Bundle获取数据	Sex = bundle.getString("Sex");
		String height = bundle.getString("ht");

		weight = bundle.getDouble("weight");

		if (Sex.equals("M")) {
			str = "你是一位男性";

			weight = (Double.parseDouble(height) - 80) * 0.7;
		} else if (Sex.equals("F")) {
			str = "你是一位女性";
			weight = (Double.parseDouble(height) - 70) * 0.6;
		}

		showResult.setText(str + "\n" + "你的身高是" + height + "厘米" + "\n"
				+ "你的标准体重是" + weight + "公斤");
//显示结果		button1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();

				intent.setClass(ShowActivity.this, MainActivity.class);

				startActivity(intent);

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

余额充值