在一个Activity中可以使用系统提供的startActivity(Intent intent)方法打开新的Activity,在打开新的Activity前,你可以决定是否为新的Activity传递参数:
第一种:打开新的Activity,不传递参数
public class HelloActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { ....... Button btnClick=(Button) this.findViewById(R.id.btnClick); btnClick.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类 startActivity(new Intent(HelloActivity.this, ShowActivity.class)); }}); } }
public class HelloActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { btnClick.setOnClickListener(new View.OnClickListener(){//点击该按钮会打开一个新的Activity public void onClick(View v) { Intent intent = new Intent(HelloActivity.this, ShowActivity.class) Bundle bundle = new Bundle();//该类用作携带数据 bundle.putString(“name”, “张三"); bundle.putInt("age", 26); intent.putExtras(bundle);//附带上额外的数据 startActivity(intent); }}); } }
在新的Activity中接收前面Activity传递过来的参数:
<pre name="code" class="html">public class ShowActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { Bundle bundle = this.getIntent().getExtras(); String name = bundle.getString("name"); int age = bundle.getInt("age");