Android--Activity在跳转时携带数据

首先看看两种传递方法示例:(一个简单姻缘计算器)

主Activity

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.   
  15.   
  16.     public void click(View v){  
  17.         Intent intent = new Intent(this, SecondActivity.class);  
  18.         //把数据封装至intent对象中  
  19. //      intent.putExtra("malename", "张三");  
  20. //      intent.putExtra("femalename", "芙蓉姐姐");  
  21.           
  22.         //把数据封装至bundle对象中  
  23.         Bundle bundle = new Bundle();  
  24.         bundle.putString("malename""<span style="font-family: Arial, Helvetica, sans-serif;">张三</span>"<span style="font-family: Arial, Helvetica, sans-serif;">);</span>  
  25.         bundle.putString("femalename""芙蓉姐姐");  
  26.           
  27.         //把bundle对象封装至intent对象中  
  28.         intent.putExtras(bundle);  
  29.         startActivity(intent);  
  30.     }  
  31.       
  32. }  
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Random;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class SecondActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_second);  
  15.           
  16.         Intent intent = getIntent();  
  17.         //从intent对象中把封装好的数据取出来  
  18. //      String maleName = intent.getStringExtra("malename");  
  19. //      String feMaleName = intent.getStringExtra("femalename");  
  20.           
  21.         Bundle bundle = intent.getExtras();  
  22.         String maleName = bundle.getString("malename");  
  23.         String feMaleName = bundle.getString("femalename");  
  24.           
  25.         Random rd = new Random();  
  26.         int yinyuan = rd.nextInt(100);  
  27.           
  28.         TextView tv = (TextView) findViewById(R.id.tv);  
  29.         tv.setText(maleName + "和" + feMaleName + "的姻缘值为" + yinyuan);  
  30.     }  
  31. }  


activity_main.xml:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"   
  10.     android:orientation="vertical"  
  11.     >  
  12.   
  13.     <TextView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="这是姻缘计算器,很准的哟" />  
  17.   
  18.     <EditText   
  19.         android:id="@+id/et_malename"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="张三"  
  23.         />  
  24.     <EditText   
  25.         android:id="@+id/et_femalename"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="芙蓉姐姐"  
  29.         />  
  30.     <Button   
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="计算"  
  34.         android:onClick="click"  
  35.         />  
  36. </LinearLayout>  


activity_second.xml:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/tv"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="这是第二个Activity" />  
  16.   
  17. </RelativeLayout>  


转载:http://blog.csdn.net/chaoyu168/article/details/51002942

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值