资源描述
Android应用开发——标准体重计开发1
实验目的
掌握android项目文件构成
掌握UI构建方式
掌握string资源文件引用
实验任务:
开发标准体重计算器Android应用,最后请输入自己的身高,提交运行截图(贴在试验报告里)
实验过程:
目标Android应用的操作过程是这样的:选择你的性别,然后输入你的身高,点查看计算结果的按钮就在Toast中显示你的标准体重。力求操作简单,结果显示清楚。
标准体重的计算公式:
男性:(身高cm-80)×70﹪=标准体重
女性:(身高cm-70)×60﹪=标准体重
按照以下步骤操作:
1,没法用真机测试的,先新建模拟器,并开启它 (会有点慢,不要紧,等你把代码写完后,肯定已经开启了,开了后就别关了,切记,因为开启它实在太浪费时间了)
2、新建android项目,命名为BMIActivity,依次设置,最好直接把最低兼容级别设置到4.0
3、然后构建UI界面:在res/layout目录下双击打开xml文件进行界面设计
实现的界面效果:
相应的XML代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<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"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sex"
/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="36px"
android:text="@string/heigh"
/>
<EditText
android:id="@+id/edit_height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/count"
/>
</LinearLayout>
其中文字引用了字符资源文件,请把res/values下的strings.xml改为如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">计算标准体重</string>
<string name="action_settings">Settings</string>
<string name="sex">请选择男女</string>
<string name="hello">计算标准体重</string>
<string name="heigh">您的身高(单位:cm)</string>
<string name="count">计算</string>
</resources>
应用的JAVA源码:注意第一行package不要复制,自己JAVA文件里package那行不要去掉
BMIActivity.java:
package com.example.bmiactivity;
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;
/*
* @author lingdududu * 该程序的功能是用户选择自己的性别和输入自己的身高,然后点击按钮,就能在Toast显示出自己的标准体重
*/
public class BMIActivity extends Activity {
/** Called when the activity is first created. */
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_bmi);
//调用创建视图的函数
creadView();
//调用性别选择的函数
sexChoose();
//调用Button注册监听器的函数
setListener();
}
//响应Button事件的函数
private void setListener() {
countButton.setOnClickListener(countListner);
}
private OnClickListener countListner = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(BMIActivity.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(){
//txt=(TextView)findViewById(R.id.txt);
countButton=(Button)findViewById(R.id.btn);
heighText=(EditText)findViewById(R.id. edit_height);
maleBtn=(RadioButton)findViewById(R.id.male);
femaleBtn=(RadioButton)findViewById(R.id.female);
//txt.setBackgroundResource(R.drawable.bg);
}
//标准体重格式化输出的函数
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;
}
}
应用效果图
大家可以根据其他复杂的标准体重计算器继续完善此应用,使其成为一个可用的、美观的Android应用。
展开阅读全文