题目要求:如果是男性,标准体重=(身高-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;
}
}