Android中利用Intent传递数据到另一个页面

在当前(主Activity)中

public class MainActivity extends ActionBarActivity {
	
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//intent可以传递一些数据到另个页面
		button=(Button) this.findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(MainActivity.this,NextActivity.class);//把数据传递到NextActivity
				
				intent.putExtra("name", "huashi");
				intent.putExtra("age", 25);
				intent.putExtra("address", "武汉");
				
				Bundle bundle=new Bundle();
				intent.putExtra("bundle", bundle);
				bundle.putString("code", "1114");
				
				startActivity(intent);//启动activity
				
			}
		});
	}

在目标Activity中

public class NextActivity extends Activity {

	private final String TAG="NextActivity";//标准的调试方法,定义要过滤的对象
	
	public NextActivity() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_next);
		//提取数据
		Intent intent=getIntent();
		String name=intent.getStringExtra("name");
		//过滤的方法,在LogCat(deprecated)的Filter中输入--进行过滤,这不是标准的调试方法
		//System.out.println("--name->>"+name);
		Log.i(TAG, "-->>"+name);
		int age=intent.getIntExtra("age", 0);
		//System.out.println("--age->>"+age);
		Log.i(TAG, "-->>"+age);
		String address=intent.getStringExtra("address");
		//System.out.println("--address->>"+address);
		Log.i(TAG, "-->>"+address);
		
		Bundle bundle=intent.getBundleExtra("bundle");
		String code=bundle.getString("code");
		//System.out.println("--code->>"+code);
		Log.i(TAG, "-->>"+code);
	}

}


你可能感兴趣的:(Android)