1. 不带数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); next=(Button)findViewById(R.id.next); next.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(MainActivity.this, Second.class); startActivity(intent); } }); }
2. 第一个Activity中数据传给第二个Activity
发送端
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); next=(Button)findViewById(R.id.next); content=(EditText)findViewById(R.id.content); next.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String name=content.getText().toString(); Intent intent=new Intent(); intent.putExtra("name", name); //Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show(); intent.setClass(MainActivity.this, Second.class); startActivity(intent); } }); }
接收端
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); name=(TextView)findViewById(R.id.name); age=(EditText)findViewById(R.id.age); back=(Button)findViewById(R.id.back); Intent intent=getIntent(); String na=intent.getStringExtra("name"); //Toast.makeText(Second.this, na, Toast.LENGTH_SHORT).show(); name.setText(na); }
3. 第二个Activity把数据传给第一个Activity
第一个Activity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); next=(Button)findViewById(R.id.next); content=(EditText)findViewById(R.id.content); next.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String name=content.getText().toString(); Intent intent=new Intent(); intent.putExtra("name", name); //Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show(); intent.setClass(MainActivity.this, Second.class); startActivityForResult(intent, RESULT_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==RESULT_CODE){ if(resultCode==Second.RESULT_CODE){ Bundle bundle=data.getExtras(); String con=bundle.getString("age"); content.setText(con); } } }
第二个Activity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); name=(TextView)findViewById(R.id.name); age=(EditText)findViewById(R.id.age); back=(Button)findViewById(R.id.back); back.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String a=age.getText().toString(); Intent intent=new Intent(); Bundle bundle=new Bundle(); bundle.putString("age", a); intent.putExtras(bundle); setResult(RESULT_CODE, intent); finish(); } }); Intent intent=getIntent(); String na=intent.getStringExtra("name"); //Toast.makeText(Second.this, na, Toast.LENGTH_SHORT).show(); name.setText(na); }