android通过intent传输数据

MainActivity
[java]
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;
}

}

}

[/java]
MainActivity01
[java]
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;
}

}
[/java]
MyData
[java]
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;
}
[/java]
项目工程文件地址:
http://pan.baidu.com/s/1o6oE7Bs

Avatar photo

About Blackford

这是个最好的时代,这是个最坏的时代,这是个充满希望的春天,这是个令人绝望的冬天,我们前面什么都有,我们前面什么都没有。梦想,让我们一次次的走远,又一次次的回头,一个关于人生的梦想还在不断奔跑,带着喜悦和疼痛,不过一切才刚刚开始,并且直到今天也远远没有结束
This entry was posted in Android开发 and tagged , . Bookmark the permalink.

发表评论

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