public class MainActivity extends Activity { Button bt; Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ...
public class MainActivity extends Activity {
Button bt;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this; //存放當前Activity的實例
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setContentView(R.layout.second);
Intent intent = new Intent(context,Second.class); //設置此Activity跳轉到另一個Activity
startActivity(intent); //啟動目標Activity
}
});
}
}
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent(); //接受起始Activity傳遞過來的Activity
}