Android 中的Intent在两个Acitvity传递数据示例

[转自] 碧天云月 的博客,开发时间紧未能及时征求同意,望大神见谅!


1.Activity02布局代码main.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 该文件是Activity02.java所使用的布局文件 -->  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8. <!-- 下面的标签声明了一个Buttin(按钮)控件,并未这个控件设置了ID,这个ID会被注册到R.java文件当中 -->  
  9. <Button  
  10.     android:id="@+id/myButton"  
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content"  
  13.     />  
  14. </LinearLayout>  

 

 

2.OtherActivity布局代码other.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 该文件是OtherActivity.java文件所使用的布局文件 -->  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8. <TextView  
  9.     android:id="@+id/myTextView"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content"  
  12.     />  
  13. </LinearLayout>  

 

3.Activity02.java

[java]  view plain  copy
  1. package mars.activity02;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. /** 
  11.  * 这个Activity是应用程序启动只有运行的第一个Activity,在这个Activity当中有一个按钮 
  12.  * @author mars_chenchuan 
  13.  * 
  14.  */  
  15. public class Activity02 extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     //代表按钮对象的引用  
  18.     private Button myButton = null;  
  19.     //复写父类当中的onCreate方法,Activity第一次运行时会调用这个方法  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         //为Activity设置布局管理文件   
  24.         setContentView(R.layout.main);  
  25.         //以下两行代码是根据控件的ID来得到控件对象  
  26.         myButton = (Button)findViewById(R.id.myButton);  
  27.         //为按钮对象设置监听器对象  
  28.         myButton.setOnClickListener(new MyButtonListener());  
  29.     }  
  30.     //以下是一个内部类,这个内部类的对象是一个监听器(如果大家对监听器不是很熟悉,可以参考设计模式当中的观察者模式)  
  31.     class MyButtonListener implements OnClickListener{  
  32.         //生成该类的对象,并将其注册到控件上。如果该控件被用户按下,就会执行onClick方法   
  33.         @Override  
  34.         public void onClick(View v) {  
  35.             //生成一个Intent对象  
  36.             Intent intent = new Intent();  
  37.             //在Intent对象当中添加一个键值对  
  38.             intent.putExtra("testIntent""123");  
  39.             //设置Intent对象要启动的Activity  
  40.             intent.setClass(Activity02.this, OtherActivity.class);  
  41.             //通过Intent对象启动另外一个Activity  
  42.             Activity02.this.startActivity(intent);  
  43.             /**以下的4行代码将启动发送短信的Activity,详细的情况会在以后的课程当中进行介绍 
  44.                 Uri uri = Uri.parse("smsto://0800000123");     
  45.                 Intent intent = new Intent(Intent.ACTION_SENDTO, uri);     
  46.                 intent.putExtra("sms_body", "The SMS text");     
  47.                 startActivity(intent); 
  48.              */  
  49.         }  
  50.           
  51.     }  
  52. }  

 

4.OtherActivty.java

[java]  view plain  copy
  1. package mars.activity02;  
  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 OtherActivity extends Activity{  
  9.     private TextView myTextView = null;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.other);  
  15.         //取得从上一个Activity当中传递过来的Intent对象  
  16.         Intent intent = getIntent();  
  17.         //从Intent当中根据key取得value  
  18.         String value = intent.getStringExtra("testIntent");  
  19.         //根据控件的ID得到响应的控件对象  
  20.         myTextView = (TextView)findViewById(R.id.myTextView);  
  21.         //为控件设置Text值  
  22.         myTextView.setText(value);  
  23.           
  24.     }  
  25.   
  26. }  

 

源代码详见:

http://download.csdn.net/source/3169414

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值