用intent实现在两个activities之间跳转
项目目录
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gogler.myAndroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ActivityIntentActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 添加第二个活动 -->
<activity
android:label="@string/app_name"
android:name=".SecondActivity" >
</activity>
</application>
</manifest>
ActivityIntentActivity.java
package gogler.myAndroid;
import gogler.myAndroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityIntentActivity extends Activity {
Button bt;
Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)this.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent();
intent.setClass(ActivityIntentActivity.this, SecondActivity.class);
ActivityIntentActivity.this.startActivity(intent);
ActivityIntentActivity.this.finish();
}
} ) ;
}
}
SecondActivity.java
package gogler.myAndroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
bt=(Button)this.findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent();
intent.setClass(SecondActivity.this,ActivityIntentActivity.class);
SecondActivity.this.startActivity(intent);
SecondActivity.this.finish();
}
} ) ;
}
}
演示: