假設手機頁面切換只需要置換背景圖示、文字內容、文字顏色及排版等等,只需要用到setContent就可以置換layout(請參考此篇http://style77125tech.pixnet.net/blog/post/13432877),但如果是要Activity的置換就不能僅靠改變layout就好,需要移交主控權到另外一個Activity,移交主控權的方法,可在主程式裡使用startActivity()這方法來呼叫另外一個Activity,但是關鍵並不是在這一個startActivity這個方法,而是在Intent(就如同英文字意一樣,想要、企圖之意)這個物件,告訴主程式自己是什麼,想要到什麼地方去,這就是intent物件所處理的事。
小試身手小範例:
Java檔 (兩個Activity兩個class)
第一個class TestExam001.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TestExam001 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main001);
Button button = (Button)findViewById(R.id.button01);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestExam001.this, TestExam002.class);
startActivity(intent);
TestExam001.this.finish();
}
});
}
}
第二個class TestExam002.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestExam002 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main002);
Button button = (Button)findViewById(R.id.button02);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestExam002.this, TestExam001.class);
startActivity(intent);
TestExam002.this.finish();
}
});
}
}
XML檔 (兩個Activity兩個對應的layout)
第一個layout main001.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="wrap_content"
android:layout_height="wrap_content"
android:text=" Activity 001"
android:textSize="50sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button01"
android:text="Go to Activity 002"
android:textSize="17sp"
/>
</LinearLayout>
AndroidManifest.xml檔
Hint:請記得在AndroidManifest.xml添增一個新的activity並給予名字,否則會出錯喔!然而在一個程式內架設有多個Activity,系統要如何決定以哪一個為entry point呢?在AndroidManifest.xml內有一行為<category android:name="android.intent.category.LAUNCHER" /> 這就表示程式會先執行這一個Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestExam001"
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=".TestExam002"></activity>
</application>
</manifest>
Ps.這兩個java程式在最後都使用finish()這方法,代表這個activity已經運行完畢,當系統接收到這一個指令的時候會關閉此activity,如果這時手機按下back按鍵,並不會回復到上一個Activity的畫面,如果需要讓使用者在按下back鑑可以回到之前的畫面,就必須將這一個finish()刪掉。

版主,請問 setContentView(R.layout.main); main跑出錯誤 main cannot be resolved or is not a field (這個不知道為什麼出了錯誤@@") intent.setClass(TestExam001.this, TestExam002.class); TestExam002跑出錯誤 cannot be resolved to a type (是因為我還沒把ACTIVITY2的程式輸入進去的關係嗎?)
只有有建立一個類似 hello world就可以跑了
其實我不太確定是用這個還是另一個呢? 我是要做一個輸入密碼 按下按鈕 判斷密碼是正確的後跳到另一個畫面去 只有有建立一個類似 hello world就可以跑了 ? (請問是需要另外再建立一個HELLO WORLD嗎?)
1.使用這一個方法就可以了,這四行就是跳到另一個Activity頁面。 從TestExam002這一個Activity跳到TestExam001這個Activity Intent intent = new Intent(); intent.setClass(TestExam002.this, TestExam001.class); startActivity(intent); TestExam002.this.finish(); 2.請記得要在AndroidManifest.xml加上第二個Activity也就是下面黃色標出處。 <activity android:name=".TestExam002"></activity> 3.兩個Activity就有兩個class
程式碼測試時沒問題,但是打開程式時都會顯示"Unfortunately,程式名has stopped" 求解 需要我把程式碼給你看嗎?
請問一下~ 從第一個activity換到第二個activity很正常 但從第二個activity換到第三個就會發生錯誤 如果使用第二個avtivity的背景圖(在XML裡:android:background)就沒問題 但想換另一張就跑不出來
你好 請問一下 我在換第二個activity也很正常 可是我在第二個activity很正常 加上按鈕 但執行卻錯誤 無法執行 程式碼如下 package tw.com.example.all; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; public class image1 extends Activity { Button B02,B03; TextView T02; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.image); B02=(Button)this.findViewById(R.id.b02); B03=(Button)this.findViewById(R.id.b03); B02.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); }}); B03.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub T02.setText("正確"); }}); } }
感謝您,解了我的燃眉之急
您好 intent.setClass(TestExam001.this, TestExam002.class); 若是我 TestExam001.this 這個 class 是一個變動的值 我想要在方法中執行,當前的 activity 不是固定的 請問應該如何做?
感謝分享~成功地轉換了 只是回到主頁之後所有變數都刷新重來了,有沒有什麼辦法讓他跳到第二個Activity時主頁處於待機狀態?
*****
若我想用兩個button轉換不同兩頁,該如何寫??