`
IT阿狸
  • 浏览: 67122 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android——体重计算器

阅读更多

这里使用的是Android 2.3.3。

 

该例子使用了相对布局RelativeLayout。

 

一、RelativeLayout相对布局

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

 

二、界面


 

 

三、该例子还使用了Intent类和Bundle类

1.Intent

a.在一个Android应用中,主要是由四种组件组成的,这四种组件可参考“Android应用的构成”。

而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。

b.在这些组件之间的通讯中,主要是由Intent协助完成的。Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

 

2.Bundle

Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap类型的变量来存放putXxx()方法放入的值.

 

 

四、main.xml(数据输入的页面)和info.xml(显示结果的页面)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="计算你/妳的标准体重"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView1"
        android:layout_below="@+id/TextView1"
        android:layout_marginTop="30dp"
        android:text="性别"
        android:textSize="20sp" >
    </TextView>

    <!-- 单选按钮组 -->
    <RadioGroup
        android:id="@+id/sexGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/TextView2"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/TextView2" >
		<!-- 单选按钮 -->
        <RadioButton
            android:id="@+id/radMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" >
        </RadioButton>

		<!-- 单选按钮 -->
        <RadioButton
            android:id="@+id/radFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" >
        </RadioButton>
    </RadioGroup>

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView1"
        android:layout_below="@+id/sexGroup"
        android:layout_marginTop="29dp"
        android:text="身高"
        android:textSize="20sp" >
    </TextView>

    <EditText
        android:id="@+id/heightNumber"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sexGroup"
        android:layout_alignTop="@+id/TextView3"
        android:hint="(单位:厘米)"
        android:inputType="numberDecimal" >
    </EditText>

    <Button
        android:id="@+id/btnCalc" 
        android:layout_centerHorizontal="true"
        android:layout_width="134dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heightNumber"
        android:layout_marginTop="37dp"
        android:layout_toRightOf="@+id/TextView2"
        android:text="计算" >
    </Button>

</RelativeLayout>

 

info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0"
        android:textSize="25sp" />

</LinearLayout>

 

 

五、Activity类,包含两个,DataTransferActivity.java(数据输入)和ShowInfoActivity.java(显示结果)

DataTransferActivity

package org.e276.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * 输入数据的Activity
 * 
 * @author miao
 * 
 */
public class DataTransferActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 计算体重的按钮
		Button btnCalc = (Button) findViewById(R.id.btnCalc);
		btnCalc.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// 得到体重的值
				EditText et = (EditText) findViewById(R.id.heightNumber);
				double height = Double.parseDouble(et.getText().toString());
				// 得到性别
				String sex = "女";
				RadioButton radMale = (RadioButton) findViewById(R.id.radMale);
				if (radMale.isChecked()) {
					sex = "男";
				}
				// 实例化一个Intent对象,并制定class
				Intent intent = new Intent();
				intent.setClass(DataTransferActivity.this,
						ShowInfoActivity.class);
				// 实例化Bundle,并将要传递的数据传入
				Bundle bundle = new Bundle();
				bundle.putDouble("height", height);
				bundle.putString("sex", sex);
				// 将Bundle对象分配给Intent对象
				intent.putExtras(bundle);
				// 调用另一个Activity
				startActivity(intent);
			}
		});
	}

}

 
ShowInfoActivity

package org.e276.activity;

import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

/**
 * 显示信息
 * 
 * @author miao
 * 
 */
public class ShowInfoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);

		// 取得Intent对象中的Bundle对象
		Bundle bundle = getIntent().getExtras();
		// 取得Bundle中的数据
		String sex = bundle.getString("sex");
		double height = bundle.getDouble("height");
		TextView tv = (TextView) findViewById(R.id.txtInfo);
		tv.setText(String.format("你是一位%s士\n你的身高是%d厘米\n你的标准体重是:%s公斤", sex,
				(int) height, getWeight(sex, height)));
	}

	/**
	 * 根据性别和身高得到体重
	 */
	private String getWeight(String sex, double height) {
		DecimalFormat f = new DecimalFormat("0.00");
		String weight = "";
		if (sex.equals("男")) {
			weight = f.format((height - 80) * 0.7);
		} else {
			weight = f.format((height - 70) * 0.6);
		}
		return weight;
	}
}

 

六、AndroidManifest.xml,因为有2个类,所有也要在配置文件里配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.e276.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/psb"
        android:label="@string/app_name" >
        <activity
            android:name=".DataTransferActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".ShowInfoActivity" android:label="显示体重" />
        
    </application>

</manifest>

 

 

七、demo

Android-CalcWeight.zip

  • 大小: 43.1 KB
分享到:
评论

相关推荐

    安卓Android源码——BMI健康计算器.zip

    在本项目中,我们关注的是一个基于安卓Android平台的源码实现——BMI(Body Mass Index,身体质量指数)健康计算器。这个应用可以帮助用户计算并理解他们的BMI值,从而评估其健康状况。以下是对这个源码项目的详细...

    android小程序 标准体重计算器示例

    以上就是开发一个Android小程序——标准体重计算器涉及的主要技术点。通过这个示例,开发者不仅可以掌握基本的Android应用开发技能,还能了解到用户输入处理、数据计算以及错误处理等方面的知识。

    试验指导5-Android应用开发——标准体重计开发.doc

    Android 应用开发是当前移动设备应用开发的热门方向之一,本文将指导读者从零开始构建一个标准体重计算器 Android 应用程序,掌握 Android 工程文件构成、UI 构建方式、string 资源文件引用等知识点。 知识点一:...

    试验指导5-android应用开发——标准体重计开发.pdf

    实验要求学生能够完成上述步骤,实现一个简单的标准体重计算器,并能正确运行在模拟器或真机上。在设计过程中,应注重用户体验,保持界面简洁,结果展示清晰。 总结来说,这个实验涵盖了Android应用开发的基础知识...

    BMI-Calculator:用于计算体重指数的简单android应用

    为了实现这个计算,我们可以创建一个Java类,如`BMIcalculator`,包含两个属性——体重和身高,以及一个方法——`calculateBMI()`。 ```java public class BMIcalculator { private double weight; private ...

    安卓BMI计算器(英文版)

    应用的核心功能是接收用户的输入——身高和体重,然后根据公式BMI = 体重(公斤)/ 身高^2(米)来计算BMI值。计算结果会精确到小数点后两位,确保数据的准确性。 应用界面通常包含两个输入框,分别对应身高和体重...

    校园生活计算器发布会流程概要1

    【校园生活计算器发布会流程概要】是一场针对校园生活实用工具——校园生活计算器的应用发布活动。这个计算器旨在为学生提供便利,集成了多种计算功能,包括基础数学计算、日期计算、图形计算、刷段预测、BMI计算...

    深入浅出android

    在Android应用中实现BMI计算器,需要定义输入字段(如体重、身高),计算BMI值,并显示结果。 以上只是Android开发的冰山一角,随着对Android SDK和相关技术的深入了解,开发者可以创建出更加复杂和创新的应用程序...

    深入浅出_Google_Android(PDF格式高清中文版)

    - **实现BMI计算**:开发一个简单的身体质量指数(BMI)计算器,用户输入身高和体重后,程序自动计算BMI值并给出健康建议。 - **界面设计**:设计简洁明了的用户界面,包括输入框和按钮等基本元素。 - **逻辑实现**...

    BMI-Calculator

    《深入解析BMI计算器项目——以Kotlin编程实现》 在当今信息化社会,健康管理已经成为人们关注的焦点。体重指数(BMI)是衡量身体健康状况的重要指标之一,它可以帮助我们了解自己的体重是否在正常范围内。本文将...

Global site tag (gtag.js) - Google Analytics