android程序一般不会只有一个activity,会碰到activity之间的跳转。以下是使用Intent做应用程序内部的activity做跳转。比如,应用程序第一个activity是:
点击“下一步”按钮后:
这需要写两个Activity类。第一个是:MainActivity
1 package com.easymorse; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity { 11 12 private Button button; 13 14 /** Called when the activity is first created. */ 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 20 this.button = (Button) this.findViewById(R.id.Button01); 21 this.button.setOnClickListener(new OnClickListener() { 22 @Override 23 public void onClick(View v) { 24 Intent intent = new Intent(); 25 intent.setClass(MainActivity.this, NextActivity.class); 26 startActivity(intent); 27 } 28 }); 29 } 30 }
第二个是:NextActivity
1 package com.easymorse; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class NextActivity extends Activity { 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 this.setContentView(R.layout.next_activity); 11 } 12 }
然后,要在AndroidManifest.xml中增加这两个Activity的声明。
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.easymorse" 4 android:versionCode="1" 5 android:versionName="1.0"> 6 <application android:icon="@drawable/icon" android:label="@string/app_name"> 7 <activity android:name=".MainActivity" 8 android:label="@string/app_name"> 9 <intent-filter> 10 <action android:name="android.intent.action.MAIN" /> 11 <category android:name="android.intent.category.LAUNCHER" /> 12 </intent-filter> 13 </activity> 14 15 <activity android:name="NextActivity"></activity> 16 </application> 17 <uses-sdk android:minSdkVersion="3" /> 18 19 </manifest>
在string.xml中增加常量字符串:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <string name="hello">Hello World, MainActivity!</string> 4 <string name="app_name">activity.forward.demo</string> 5 <string name="next_button">下一步</string> 6 </resources>
layout目录下除了原有的main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" 11 /> 12 <Button android:text="@string/next_button" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 13 </LinearLayout>
还需要创建NextActivity的布局文件声明,比如next_activity.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content"> 6 <TextView android:text="@string/next_button" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 7 </LinearLayout>
这种情况下,如果按回退键将回到MainActivity。如果不希望回退到前一个activity,而是退出。需要这样:
1 this.button = (Button) this.findViewById(R.id.Button01); 2 this.button.setOnClickListener(new OnClickListener() { 3 @Override 4 public void onClick(View v) { 5 Intent intent = new Intent(); 6 intent.setClass(MainActivity.this, NextActivity.class); 7 startActivity(intent); 8 finish(); 9 } 10 });