android activity通过全局对象传递数据

  • 内容
  • 评论
  • 相关

MainActivity

package com.example.clipboard;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button01 = (Button) findViewById(R.id.button1);
		button01.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.button1:
			// TODO Auto-generated method stub
			MyApp myApp = (MyApp) getApplicationContext();
			myApp.city = "hangzhou";
			myApp.data.id = 123456;
			myApp.data.name = "hellogxp";
			intent = new Intent(this,MainActivity01.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}

}

MainActivity01

package com.example.clipboard;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity01 extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity01);
		
		TextView textView01 = (TextView) findViewById(R.id.textview01);
		MyApp myApp = (MyApp) getApplicationContext();
		
		textView01.setText("MyApp.city: " + myApp.city + "\nmyApp.data.id" + myApp.data.id + "\nmyApp.data.name" + myApp.data.name);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_activity01, menu);
		return true;
	}

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.clipboard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:name=".MyApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.clipboard.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.clipboard.MainActivity01"
            android:label="@string/title_activity_main_activity01" >
        </activity>
    </application>

</manifest>

完整项目工程文件
http://pan.baidu.com/s/1pJjmP9h

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注