实现android activity之间的跳转

android程序一般不会只有一个activity,会碰到activity之间的跳转。以下是使用Intent做应用程序内部的activity做跳转。比如,应用程序第一个activity是:

实现android activity之间的跳转

点击“下一步”按钮后:

实现android activity之间的跳转

这需要写两个Activity类。第一个是:MainActivity

 package com.easymorse;

 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 MainActivity extends Activity { private Button button; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); this.button = (Button) this.findViewById(R.id.Button01);
this.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, NextActivity.class);
startActivity(intent);
}
});
}
}

第二个是:NextActivity

 package com.easymorse;

 import android.app.Activity;
import android.os.Bundle; public class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.next_activity);
}
}

然后,要在AndroidManifest.xml中增加这两个Activity的声明。

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.easymorse"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name="NextActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="3" /> </manifest>

在string.xml中增加常量字符串:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">activity.forward.demo</string>
<string name="next_button">下一步</string>
</resources>

layout目录下除了原有的main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@string/next_button" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

还需要创建NextActivity的布局文件声明,比如next_activity.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/next_button" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

这种情况下,如果按回退键将回到MainActivity。如果不希望回退到前一个activity,而是退出。需要这样:

         this.button = (Button) this.findViewById(R.id.Button01);
this.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
});

实现activity之间的跳转.rar

上一篇:开发错误记录七: Failed to create JVM:error code -4


下一篇:html 7.29