使用getXXXExtra()系列方法从Intent对象中取出数据
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="启动第二个Activity"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.android.xiong.intent_one; 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 { private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); OnClick on=new OnClick(); button1.setOnClickListener(on); } class OnClick implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent =new Intent(); intent.setClass(MainActivity.this, TwoActivity.class); String [] name={"zhangsan","lis","wangwu"}; int []age={12,13,14}; intent.putExtra("com.android.xiong.intent_one.Name", name); intent.putExtra("com.android.xiong.intent_one.Age", age); 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 com.android.xiong.intent_one; import java.util.HashMap; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class TwoActivity extends Activity{ private TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); text1=(TextView)findViewById(R.id.text1); Intent intent=getIntent(); String []name=intent.getStringArrayExtra("com.android.xiong.intent_one.Name"); int [] age=intent.getIntArrayExtra("com.android.xiong.intent_one.Age"); String print=""; for (int i = 0; i < age.length; i++) { print+="name: "+name[i].toString()+" age: "+age[i]+" \n"; } text1.setText(print); } }