首页 > 代码库 > Intent ,Bundle 组件 两个Activity 互相传值
Intent ,Bundle 组件 两个Activity 互相传值
第一个Activity
1 /** 2 * 3 */ 4 package com.flysnow.sina.weibo; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View;10 import android.widget.Button;11 import android.widget.EditText;12 import android.widget.RadioButton;13 import android.widget.TextView;14 import android.widget.Toast;15 16 /**17 * 首页Activity18 * @author 19 * @since 2011-3-820 */21 public class HomeActivity extends Activity {22 23 @Override24 protected void onCreate(Bundle savedInstanceState) {25 super.onCreate(savedInstanceState);26 setContentView(R.layout.login_table); 27 28 Button login= (Button) findViewById(R.id.login);29 30 login.setOnClickListener(new Button.OnClickListener() {31 32 @Override33 public void onClick(View v) {34 // TODO Auto-generated method stub35 try {36 EditText u = (EditText) findViewById(R.id.username);37 EditText p = (EditText) findViewById(R.id.password);38 EditText h = (EditText) findViewById(R.id.height);39 RadioButton s= (RadioButton) findViewById(R.id.sex_man);40 String username=u.toString();41 String passwd=p.toString();42 double height= Double.parseDouble(h.getText().toString()); //转换为double类型 43 String sex;44 if(s.isChecked()){45 sex ="男性";46 }else{47 48 sex ="女性";49 }50 Intent intent=new Intent();51 intent.setClass(HomeActivity.this, MoreActivity.class);52 Bundle bu=new Bundle(); // 这个组件 存值53 bu.putString("username", username);54 bu.putString("passwd", passwd);55 bu.putString("sex", sex);56 bu.putDouble("height", height);57 intent.putExtras(bu); //放到 intent 里面 然后 传出去58 startActivity(intent);59 }catch(Exception e){60 // TODO: handle exception61 Toast.makeText(HomeActivity.this,"errorrrrr", Toast.LENGTH_LONG).show();62 }63 64 }});65 66 };67 68 //从传过去的页面返回数据 的方法69 protected void onActivityResult(int requestCode, int resultCode,Intent data) {70 // TODO Auto-generated method stub71 super.onActivityResult(requestCode, resultCode, data);72 RadioButton radiobutton_Man= (RadioButton) findViewById(R.id.sex_man);73 RadioButton radiobutton_Woman= (RadioButton) findViewById(R.id.sex_woman);74 EditText edit_height = (EditText) findViewById(R.id.height);75 switch (resultCode) {76 case RESULT_OK:77 /* 取得来自Activity2 的数据,并显示于画面上*/78 Bundle bunde = data.getExtras();79 String sex = bunde.getString("sex");80 double height = bunde.getDouble("height");81 edit_height.setText("" + height);82 if (sex.equals("男性")) {83 radiobutton_Man.setChecked(true);84 } else {85 radiobutton_Woman.setChecked(true);86 } 87 break;88 89 default:90 break;91 }92 }93 94 95 }
第二个Activity
1 /** 2 * 3 */ 4 package com.flysnow.sina.weibo; 5 6 7 import java.text.DecimalFormat; 8 import java.text.NumberFormat; 9 10 import android.app.Activity;11 import android.content.Intent;12 import android.os.Bundle;13 import android.view.View;14 import android.widget.Button;15 import android.widget.TextView;16 17 /**18 * 更多Activity19 * @author 20 * @since 2011-3-821 */22 public class MoreActivity extends Activity {23 24 private Intent intent;25 private Bundle budle;26 @Override27 protected void onCreate(Bundle savedInstanceState) {28 super.onCreate(savedInstanceState);29 30 setContentView(R.layout.layout2); 31 32 TextView a=(TextView) findViewById(R.id.textView111);33 34 intent= this.getIntent(); //接受的数据35 budle =intent.getExtras();36 37 String sex =budle.getString("sex");38 double height = budle.getDouble("height");39 String tizhong = this.getWeight(sex, height);40 41 /*使用CharSequence类getString()方法从XML中获取String*/42 CharSequence string2=getString(R.string.app_name);43 44 45 String z = "("+string2+")你是一个"+sex+",你的身高为"+height+"你的标准体重为"+tizhong;46 a.setText(z);47 48 Button back= (Button) findViewById(R.id.back); //返回事件49 back.setOnClickListener(new Button.OnClickListener() {50 51 @Override52 public void onClick(View v) {53 // TODO Auto-generated method stub54 //返回result 回上一个activity 55 MoreActivity.this.setResult(RESULT_OK, intent);56 //结束这个activity 57 MoreActivity.this.finish();58 }59 });60 61 }62 63 /* 四舍五入的method */64 private String format(double num) {65 NumberFormat formatter = new DecimalFormat("0.00");66 String s = formatter.format(num);67 return s;68 }69 70 private String getWeight(String sex, double height) {71 String weight = "";72 if (sex.equals("男性")) {73 weight = format((height - 80) * 0.7);74 } else {75 weight = format((height - 70) * 0.6);76 } 77 78 return weight;79 }80 81 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。