实验二_任务二_标准体重计算器
来源:互联网 发布:淘宝帐号更换支付宝 编辑:程序博客网 时间:2024/05/03 19:56
实验2 Activity 和Intent
1. 实验目的
1. 掌握如何创建一个Activity
2. 掌握如何在Activity中添加控件并进行事件处理
3. 掌握如何通过Intent启动另外一个Activity
4. 掌握如何通过Intent在多个Activity之间传递数据
5. 掌握Activity的生命周期
2. 实验仪器
装有Android开发环境的计算机
任务2:标准体重计算器
如果是男性,标准体重=(身高-80)*0.7
如果是女性,标准体重=(身高-70)*0.6
1. 运行效果图
2. 训练目标
1) 掌握RadioButton组件的使用
2) Intent的使用
3) 掌握如何在不同Activity之间通过Bundle传递数据
3.核心代码
①MainActivity
[java] view plain copy
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
etName=(EditText) findViewById(R.id.etName);
gro=(RadioGroup) findViewById(R.id.radioGroup1);
gro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int data) {
switch (data) {
case R.id.rbmale:
Sex="男性";
break;
case R.id.rbfemale:
Sex="女性";
default:
break;
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String High=etName.getText().toString();
Intent intent=new Intent();
intent.setClass(getApplicationContext(), ShowActivity.class);
Bundle bundle=new Bundle();
bundle.putString("High", High);
bundle.putString("Sex", Sex);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
②ShowActivity
[java] view plain copy
<pre name="code" class="java">public class ShowActivity extends Activity {
private double Weight;
private TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
tvShow=(TextView) findViewById(R.id.tvShow);
Intent intent =getIntent();
Bundle bundle=getIntent().getExtras();
String Sex=bundle.getString("Sex");
String High=bundle.getString("High");
if(Sex.equals("男性"))
{
Weight=((Integer.parseInt(High))-80)*0.7;
}
else
{
Weight=((Integer.parseInt(High))-70)*0.6:
}
tvShow.setText("您是一名"+Sex+"\n身高为:"+High+"cm\n标准体重为:"+Weight+"千克");
4.问题与反思
[java] view plain copy
gro=(RadioGroup) findViewById(R.id.radioGroup1);
gro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">}</span>
1. 实验目的
1. 掌握如何创建一个Activity
2. 掌握如何在Activity中添加控件并进行事件处理
3. 掌握如何通过Intent启动另外一个Activity
4. 掌握如何通过Intent在多个Activity之间传递数据
5. 掌握Activity的生命周期
2. 实验仪器
装有Android开发环境的计算机
任务2:标准体重计算器
如果是男性,标准体重=(身高-80)*0.7
如果是女性,标准体重=(身高-70)*0.6
1. 运行效果图
2. 训练目标
1) 掌握RadioButton组件的使用
2) Intent的使用
3) 掌握如何在不同Activity之间通过Bundle传递数据
3.核心代码
①MainActivity
[java] view plain copy
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
etName=(EditText) findViewById(R.id.etName);
gro=(RadioGroup) findViewById(R.id.radioGroup1);
gro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int data) {
switch (data) {
case R.id.rbmale:
Sex="男性";
break;
case R.id.rbfemale:
Sex="女性";
default:
break;
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String High=etName.getText().toString();
Intent intent=new Intent();
intent.setClass(getApplicationContext(), ShowActivity.class);
Bundle bundle=new Bundle();
bundle.putString("High", High);
bundle.putString("Sex", Sex);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
②ShowActivity
[java] view plain copy
<pre name="code" class="java">public class ShowActivity extends Activity {
private double Weight;
private TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
tvShow=(TextView) findViewById(R.id.tvShow);
Intent intent =getIntent();
Bundle bundle=getIntent().getExtras();
String Sex=bundle.getString("Sex");
String High=bundle.getString("High");
if(Sex.equals("男性"))
{
Weight=((Integer.parseInt(High))-80)*0.7;
}
else
{
Weight=((Integer.parseInt(High))-70)*0.6:
}
tvShow.setText("您是一名"+Sex+"\n身高为:"+High+"cm\n标准体重为:"+Weight+"千克");
4.问题与反思
[java] view plain copy
gro=(RadioGroup) findViewById(R.id.radioGroup1);
gro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">}</span>
0 0
- 实验二_任务二_标准体重计算器
- 初学移动开发技术_实验二_任务二_标准体重计算器
- 实验二.任务二.标准体重计算器
- 实验二任务二标准体重计算器
- 实验二 任务2 标准体重计算器
- 任务二 标准体重计算器
- 任务二:标准体重计算器
- 实验二—任务2:标准体重计算器--------------闪退
- 实验二 Intent 简单标准体重计算器
- android实验二之体重标准计算器
- 实验二——任务二——标准体重计算器(出错待改)
- 实验任务2 标准体重计算器
- 任务二计算标准体重
- 实验二 任务二 体重测量
- 实验2 Activity和Intent-任务2:标准体重计算器
- 实验二_任务三_理解Task
- 实验1 标准体重计算器
- 实验1:标准体重计算器
- JSON的一些简单运用
- UGUI研究院之判断鼠标或者手指是否点击在UI上
- struts2请求的执行顺序
- 博客迁移<a>jiangweili.me</a>
- 国家集训队论文分类整理
- 实验二_任务二_标准体重计算器
- 快捷编程好工具——Go语言
- linux中原子量的学习和理解
- Java中float、double、long类型变量赋值添加f、d、L尾缀问题
- BestCoder Round #87 1003 && hdu 5904(递推
- hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- 使用BeanUtils复制bean的属性,和将request的属性设置到bean中
- 上拉,下拉的布局
- Linux运维+系统服务搭建(一)bash命令及其特性