使用此方法一般需要两个activity文件,而且必须要在Manifest文件中注册Activity信息。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this) ;
final Button btn1 = new Button(this) ;
ll.addView(btn1) ;
btn1.setText("Start Intent") ;
setContentView(ll) ;
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent() ;
intent.setClass(MainActivity.this, AnotherActivity.class) ;
startActivity(intent) ;
MainActivity.this.finish() ;
}
}) ;
}
//************* 另一个Activity
public class AnotherActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another) ;
}
}
```xml
<?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/activity_another"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity another" />
</LinearLayout>