這兩天閑下來,想隨便寫點android程式,突然發現,之前忙了大半年的android,居然連個簡單的遊戲都做不了,看來基礎確實很重要,開始從最實學習。今天研究兩個activity切換,應該用到startActivity(Intent aIntent)。
兩個activity儘可能簡單,第一個包含一個button按鈕,點擊按鈕,調用startActivity()進行切換。
先看AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.szkingdom.uinttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Sample8Activity" >
<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" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
再看第一個activity,由於需要一個響應按鈕事件,因此在onCreate中需加上如下兩行代碼:
// Watch for button clicks
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(mFadeListener);
通過這個內部匿名類實現按鈕響應,轉到第二activity中:
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Sample8Activity.this, SecondActivity.class));
}
};
第一個activity的代碼如下所示:
package com.szkingdom.uinttest;
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 Sample8Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Watch for button clicks
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(mFadeListener);
}
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Sample8Activity.this, SecondActivity.class));
}
};
}
第二個activity代碼只是一個簡單的顯示,如下所示:
package com.szkingdom.uinttest;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsecond);
}
}
相應的,layout中兩個檔案main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
mainsecond.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/helloSecond" />
</LinearLayout>
strings.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sample8Activity!</string>
<string name="helloSecond">Hello World, helloSecond!</string>
<string name="app_name">Sample8</string>
</resources>
至此,一個簡單的activity切換就完成了,由於是入門級的,因此非常詳細,非常適合像我一樣半路出家的朋友,希望對大家有協助。