android通过intent传输数据

  • 内容
  • 评论
  • 相关

MainActivity

package com.example.settitle;

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

public class MainActivity extends Activity implements OnClickListener{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.button1:
			intent = new Intent(this,MainActivity01.class);
			MyData myData = new MyData();
			myData.id = 900000;
			myData.name = "月薪";
			intent.putExtra("key1", "春节快乐");
			intent.putExtra("key2", 123456);
			intent.putExtra("myData", myData);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}

}

MainActivity01

package com.example.settitle;

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 textView = (TextView) findViewById(R.id.textview01);
		
		String intentString = getIntent().getStringExtra("key1");
		Integer intentInt = getIntent().getExtras().getInt("key2");
		MyData intentMyData = (MyData) getIntent().getExtras().get("myData");
		
		StringBuffer sb = new StringBuffer();
		sb.append("key01:");
		sb.append(intentString);
		sb.append("\n");
		sb.append("key02:");
		sb.append("\n");
		sb.append(intentMyData.id);
		sb.append(intentMyData.name);
		sb.append(intentInt);
		
		textView.setText(sb.toString());
	}

	@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;
	}

}

MyData

package com.example.settitle;

import java.io.Serializable;

public class MyData implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Integer id;
	public String name;
}

项目工程文件地址:
http://pan.baidu.com/s/1o6oE7Bs

评论

0条评论

发表评论

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