Intent--打开另一个Activity---单项传值

本文介绍如何在Android应用中实现不同Activity间的跳转,并通过Intent传递数据。示例包括MainActivity与OtherActivity的布局文件及Java代码,展示了按钮点击触发Activity跳转并传递字符串值的过程。


在Android应用中实现activity之间的跳转使用intent机制。

  本例子简单地简绍如何利用intent使程序由MainActivity跳转到另一个OtherActivity并实现单向传值。

一、设计界面

  1、MainActivity布局文件

  打开res/layout/activity_main.xml文件。
  输入以下代码:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/open"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="打开另一个Activity" />  
  13.   
  14. </LinearLayout>  


  2、OtherActivity布局文件

  打开res/layout/otheractivity.xml文件。
  输入以下代码:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/close"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="关闭当前Activity(OtherActivity)" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/orderno"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="显示从MainActivity的传值!" />  
  18.   
  19. </LinearLayout>  


二、程序文件

  1、打开“src/com.genwoxue.contentprovider_b/MainActivity.java”文件。
  然后输入以下代码:

[java]  view plain  copy
  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.   
  15. private Button btnOpen=null;  
  16. @Override  
  17. public void onCreate(Bundle savedInstanceState)     
  18.     {     
  19.         super.onCreate(savedInstanceState);                
  20.         setContentView(R.layout.activity_main);      
  21.         btnOpen=(Button)super.findViewById(R.id.open);  
  22.         btnOpen.setOnClickListener(new OnClickListener(){  
  23.         public void onClick(View v)  
  24.         {    
  25.         Intent intent=new Intent(MainActivity.this,OtherActivity.class);  
  26.         intent.putExtra("info""No66778899");  
  27.         MainActivity.this.startActivity(intent);  
  28.         }  
  29.         });  
  30.           
  31.     }  
  32. }         
  33.       
  34.       

  2、打开“src/com.genwoxue.contentprovider_b/OtherActivity.java”文件。
  然后输入以下代码:

[java]  view plain  copy
  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12.   
  13. public class OtherActivity extends Activity {  
  14.   
  15.   
  16. private TextView tvOrderNo=null;  
  17. private Button btnClose=null;  
  18. @Override  
  19. public void onCreate(Bundle savedInstanceState)     
  20.     {     
  21.         super.onCreate(savedInstanceState);                
  22.         setContentView(R.layout.otheractivity);             
  23.         tvOrderNo=(TextView)super.findViewById(R.id.orderno);  
  24.         btnClose=(Button)super.findViewById(R.id.close);  
  25.           
  26.         Intent intent=super.getIntent();  
  27. String orderNo=intent.getStringExtra("info");  
  28. tvOrderNo.setText(orderNo);  
  29. btnClose.setOnClickListener(new OnClickListener(){  
  30.         public void onClick(View v)  
  31.         {    
  32.         finish();  
  33.         }  
  34.         });  
  35.     }  
  36. }            
  37.       

三、配置文件

  打开“AndroidManifest.xml”文件。
  然后输入以下代码:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.intent_a"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.genwoxue.intent_a.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.           
  26.        <strong><span style="color:#ff0000;"> <activity  
  27.             android:name="com.genwoxue.intent_a.OtherActivity">  
  28.         </activity>  
  29. </span></strong>    </application>  
  30.   
  31. </manifest>  


 

四、运行结果

   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值