Android中两个Activity之间简单的传递Extra

 1 package com.wizzhangquan.helloworld;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.TextView;
11 
12 public class MainActivity extends Activity {
13 
14     private Button myButton = null;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         TextView myTextView = (TextView)findViewById(R.id.myTextView);
20         myButton=(Button)findViewById(R.id.myButton);
21         myTextView.setText("My first TextView");
22         myButton.setText("My first Button");
23         myButton.setOnClickListener(new MyButtonListener());
24     }
25 
26     @Override
27     public boolean onCreateOptionsMenu(Menu menu) {
28         // Inflate the menu; this adds items to the action bar if it is present.
29         getMenuInflater().inflate(R.menu.main, menu);
30         return true;
31     }
32 
33     class MyButtonListener implements OnClickListener {
34 
35         @Override
36         public void onClick(View v) {
37             // TODO Auto-generated method stub
38             Intent intent = new Intent();
39             intent.putExtra("testIntent", "123456");
40             intent.setClass(MainActivity.this, OtherActivity.class);
41             MainActivity.this.startActivity(intent);
42         }
43         
44     }
45 }
 1 package com.wizzhangquan.helloworld;
 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 
10     private TextView otherTextView = null;
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         // TODO Auto-generated method stub
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.other_activity);
16         Intent intent = getIntent();//获取intent
17         String Value = intent.getStringExtra("testIntent");
18         otherTextView = (TextView)findViewById(R.id.otherTextView);
19         otherTextView.setText(Value);
20     }
21 
22 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.wizzhangquan.helloworld"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="14" />
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.wizzhangquan.helloworld.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         <activity
26             android:name=".OtherActivity"
27             android:label="@string/other_activity">
28         </activity>
29     </application>
30 
31 </manifest>

 

posted on 2013-03-08 22:40  Zachary_wiz  阅读(656)  评论(0编辑  收藏  举报

导航