import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickBtn(View view)
{
if(view.getId()==R.id.btn1)
{
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("name", "张三");
intent.putExtra("age",21);
intent.putExtra("score", 78.5);
Student stu=new Student();
stu.setName("李四");
stu.setAge(18);
stu.setScore(95.5);
intent.putExtra("stu",stu);
startActivity(intent);
}
}
@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;
}
}
package veryedu.cls9.lession10.extra;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
Bundle bundle=intent.getExtras();
int age=bundle.getInt("age");
double score=bundle.getDouble("score");
Student stu=(Student)bundle.getSerializable("stu");
Toast.makeText(this, stu.toString(), Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
package veryedu.cls9.lession10.extra;
import java.io.Serializable;
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private double score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String toString()
{
return "姓名:"+name+" 年龄:"+age+" 分数"+score;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="veryedu.cls9.lession10.extra"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="veryedu.cls9.lession10.extra.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="veryedu.cls9.lession10.extra.SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
</manifest>