本文主要介绍 Bundle传值,具体操作看代码。
1.Bundle1Activity.java
/**
*
* 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity
*/
public class Bundle1Activity extends Activity {
private int my_requestCode=1550;
private RadioButton sexMan;
private RadioButton sexWoman;
private EditText heightEdit;
private Button okButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle1);
sexMan=(RadioButton)findViewById(R.id.sex_man);
sexWoman=(RadioButton)findViewById(R.id.sex_woman);
heightEdit=(EditText)findViewById(R.id.height_edit);
okButton=(Button)findViewById(R.id.button_ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double height=Double.parseDouble(heightEdit.getText().toString());
String sex="";
if(sexMan.isChecked()){
sex="M";
}else{
sex="F";
}
Intent intent=new Intent();
intent.setClass(Bundle1Activity.this, Bundle2Activity.class);
//封装数据
Bundle bundle=new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
intent.putExtras(bundle);
startActivityForResult(intent, my_requestCode);
}
});
}
//回调方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_OK:
//RESULT_OK表示返回成功,提取数据。
Bundle bundle=data.getExtras();
String sex=bundle.getString("sex");
double height=bundle.getDouble("height");
heightEdit.setText(String.valueOf(height));
if(sex.equals("M")){
sexMan.setChecked(true);
}else{
sexWoman.setChecked(true);
}
break;
default:
break;
}
}
}
2.Bundle2Activity.java
public class Bundle2Activity extends Activity{
private TextView result;
private Button backButton;
Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle2);
result=(TextView)findViewById(R.id.result);
backButton=(Button)findViewById(R.id.button_back);
intent=this.getIntent();
Bundle bundle=intent.getExtras();
double height=bundle.getDouble("height");
String sex=bundle.getString("sex");
String sexStr="";
if("M".equals(sex)){
sexStr="男性";
}else{
sexStr="女性";
}
String weight=this.getWeight(sex, height);
result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤");
//返回上一页
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle2Activity.this.setResult(RESULT_OK, intent);
Bundle2Activity.this.finish();
}
});
}
public String getWeight(String sex,double height){
String weight="";
if("M".endsWith(sex)){
weight=format((height-80)*0.7);
}else{
weight=format((height-70)*0.6);
}
return weight;
}
public String format(double num){
NumberFormat formatter=new DecimalFormat("0.00");
String s=formatter.format(num);
return s;
}
}
3.布局文件bundle1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/showText"
android:layout_width="wrap_content"
android:layout_height="26px"
android:textSize="20px"
android:layout_x="65px"
android:layout_y="21px"
android:text="计算你的标准体重"/>
<TextView
android:id="@+id/text_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="71px"
android:layout_y="103px"
android:text="性别"/>
<TextView
android:id="@+id/text_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="72px"
android:layout_y="169px"
android:text="身高"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="37px"
android:orientation="horizontal"
android:layout_x="124px"
android:layout_y="101px">
<RadioButton
android:id="@+id/sex_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"/>
<RadioButton
android:id="@+id/sex_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<EditText
android:id="@+id/height_edit"
android:layout_width="80px"
android:layout_height="wrap_content"
android:numeric="decimal"
android:textSize="18sp"
android:layout_x="124px"
android:layout_y="160px"
android:textCursorDrawable="@null" />
<Button
android:id="@+id/button_ok"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="计算"
android:layout_x="125px"
android:layout_y="263px"/>
</AbsoluteLayout>
4.布局文件bundle2.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:layout_x="10px"
android:layout_y="21px"
android:text=""/>
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回上一页"
android:layout_x="25px"
android:layout_y="193px"/>
</AbsoluteLayout>
5.注意:需要在AndroidManifest.xml注册相应Activity.