在一般情況下,Android的每一個屏幕基本上就是一個活動(Activity),屏幕之間的切換實際上就是在活動間互相調用的過程,Android使用Intent完成這個動作。
事實上,在Android中,屏幕使用一個活動來實現,屏幕之間是相互獨立的,屏幕之間的跳轉關係通過Intent來實現。
本示例是一個簡單的屏幕之間的跳轉,從一個屏幕跳轉到另一個屏幕,在啓動第二個屏幕後,前一個屏幕消失。
1、添加頁面
2、跳轉代碼
Forward
package com.hanji.forward;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Forward extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forward);
final Button btn_Next = (Button) findViewById(R.id.button1);
btn_Next.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* 跳轉方法 */
Intent intent = new Intent(); // 建立Intent
intent.setClass(Forward.this, ForwardTarget.class); // 設置活動
startActivity(intent);
finish();
}
});
}
}
activity_forward.xml代碼如下:
package com.hanji.forward;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Forward extends ActionBarActivity {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_forward);
<span style="white-space:pre"> </span>final Button btn_Next = (Button) findViewById(R.id.button1);
<span style="white-space:pre"> </span>btn_Next.setOnClickListener(new Button.OnClickListener() {
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>/* 跳轉方法 */
<span style="white-space:pre"> </span>Intent intent = new Intent(); // 建立Intent
<span style="white-space:pre"> </span>intent.setClass(Forward.this, ForwardTarget.class); // 設置活動
<span style="white-space:pre"> </span>startActivity(intent);
<span style="white-space:pre"> </span>finish();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>}
}
activity_forward_target.xml代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanji.forward.ForwardTarget" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SecondPage" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="@string/Pres" />
</RelativeLayout>
ForwardTarget.java代碼如下:
package com.hanji.forward;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ForwardTarget extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forward_target);
final Button btn_Next = (Button) findViewById(R.id.button1);
btn_Next.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* 跳轉方法 */
Intent intent = new Intent(); // 建立Intent
intent.setClass(ForwardTarget.this, Forward.class); // 設置活動
startActivity(intent);
finish();
}
});
}
}
string.xml頁面代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Forward</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_forward_target">ForwardTarget</string>
<string name="Pres">轉到第一界面</string>
<string name="Nexts">轉到第二界面</string>
<string name="FirstPage">這裏是第一界面</string>
<string name="SecondPage">這裏是第二界面</string>
</resources>
效果如下所示: