<Activity1.java>
2:
3: import android.app.Activity;
4: import android.content.Intent;
5: import android.os.Bundle;
6: import android.view.View;
7: import android.widget.Button;
8:
9: public class Activity1 extends Activity {
10: /** Called when the activity is first created. */
11: @Override
12: public void onCreate(Bundle savedInstanceState) {
13: super.onCreate(savedInstanceState);
14: setContentView(R.layout.activity1layout);
15:
16: Button b1 = (Button) findViewById(R.id.btnSwitchto2);
17: Button b2 = (Button) findViewById(R.id.btnExit);
18: b1.setOnClickListener(new View.OnClickListener() {
19: public void onClick(View v) {
20: Intent i = new Intent();
21: i.setClass(Activity1.this, Activity2.class);
22: startActivity(i);
23: }
24: });
25:
26: b2.setOnClickListener(new View.OnClickListener() {
27: public void onClick(View v) {
28: android.os.Process.killProcess(android.os.Process.myPid());
29: finish();
30: }
31: });
32: }
33: }
<Activity2.java>
1: package com.android.switchbetweenactivities;
2:
3: import android.app.Activity;
4: import android.content.Intent;
5: import android.graphics.Color;
6: import android.os.Bundle;
7: import android.view.View;
8: import android.widget.Button;
9: import android.widget.LinearLayout;
10:
11: public class Activity2 extends Activity {
12: /** Called when the activity is first created. */
13: @Override
14: public void onCreate(Bundle savedInstanceState) {
15: super.onCreate(savedInstanceState);
16: setContentView(R.layout.activity2layout);
17:
18: final LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
19: layout2.setBackgroundColor(Color.BLUE);
20:
21: Button b1 = (Button) findViewById(R.id.btnReturn);
22: b1.setOnClickListener(new View.OnClickListener() {
23: public void onClick(View v) {
24: Intent i = new Intent();
25: setResult(RESULT_OK, i);
26: finish();
27: }
28: });
29: }
30: }
<activity1layout.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:orientation="vertical" >
6:
7: <TextView
8: android:layout_width="fill_parent"
9: android:layout_height="wrap_content"
10: android:text="@string/strLayout1" />
11:
12: <RelativeLayout
13: android:id="@+id/relativeLayout1"
14: android:layout_width="match_parent"
15: android:layout_height="wrap_content" >
16:
17: <Button
18: android:id="@+id/btnSwitchto2"
19: android:layout_width="wrap_content"
20: android:layout_height="wrap_content"
21: android:layout_alignParentLeft="true"
22: android:layout_alignParentTop="true"
23: android:text="@string/strSwitchto2" />
24:
25: <Button
26: android:id="@+id/btnExit"
27: android:layout_width="wrap_content"
28: android:layout_height="wrap_content"
29: android:layout_alignParentTop="true"
30: android:layout_toRightOf="@+id/btnSwitchto2"
31: android:text="@string/strExit" />
32:
33: </RelativeLayout>
34:
35: </LinearLayout>
<activity2layout.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:id="@+id/layout2"
4: android:layout_width="match_parent"
5: android:layout_height="match_parent"
6: android:orientation="vertical" >
7:
8: <TextView
9: android:layout_width="fill_parent"
10: android:layout_height="wrap_content"
11: android:text="@string/strLayout2" />
12:
13: <Button
14: android:id="@+id/btnReturn"
15: android:layout_width="wrap_content"
16: android:layout_height="wrap_content"
17: android:text="@string/strSwitchto1" />
18:
19: </LinearLayout>
<strings.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3:
4: <string name="hello">Hello World, SwitchBetweenActivities!</string>
5: <string name="app_name">SwitchBetweenActivities</string>
6: <string name="strLayout1">這是 Activity1 的 Layout 畫面!</string><string name="strLayout2">這是 Activity2 的 Layout 畫面!</string>
7: <string name="strSwitchto1">返回 Activity 1</string>
8: <string name="strSwitchto2">跳至 Activity2</string><string name="strExit">離開</string>
9:
10: </resources>
<AndroidManifest.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3: package="com.android.switchbetweenactivities"
4: android:versionCode="1"
5: android:versionName="1.0" android:installLocation="auto">
6:
7: <uses-sdk android:minSdkVersion="8" />
8:
9: <application
10: android:icon="@drawable/ic_launcher"
11: android:label="@string/app_name" >
12: <activity
13: android:name=".Activity1"
14: android:label="@string/app_name" >
15: <intent-filter>
16: <action android:name="android.intent.action.MAIN" />
17:
18: <category android:name="android.intent.category.LAUNCHER" />
19: </intent-filter>
20: </activity>
21: <activity android:name="Activity2"></activity>
22: </application>
23:
24: </manifest>
沒有留言:
張貼留言