Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

透過實例快速掌握 Android Activity 組件

編輯:關於 Android 程式設計

Activity 是 Android 應用中最直接與使用者接觸的組件,它負責加載 View 組件並展現給使用者,同時保持與使用者的互動。所有 Activity 組件都需要繼承 Activity 類,這是 Context 的間接子類,包裝了一些 Activity 的基本特性。

View 組件是所有 UI 組件、容器組件的基類,也就是說,它可以是一個版面配置容器,也可以是容器內的基本 UI 組件。View 組件一般透過 XML 版面配置資源檔定義,同時 Android 系統也為這些 View 組件提供了對應的實現類。如果需要透過某個 Activity 把指定的 View 組件顯示出來,呼叫 Activity 的 setContentView() 方法即可,它有多個重載方法,可以傳遞一個 XML 資源 ID 或 View 物件。

例如:

 LinearLayout layout=new LinearLayout(this); setContentView(layout); 

或者:

 setContentView(R.layout.main); 

Activity 為 Android 應用提供了一個使用者介面。當一個 Activity 被開啟後,它就有自己的生命週期,Activity 類也為這些生命週期提供了對應的方法。如果需要對 Activity 各個不同的生命週期做出響應,可以重寫這些方法來實現。對於大多數商業應用而言,整個系統包含多個 Activity,在應用中逐步導航跳轉開啟這些 Activity 後,會形成 Activity 的回退堆疊,當前顯示並獲得焦點的 Activity 位於這個堆疊的棧頂。

實例

一、如何定義多個 Activity

1. 定義一個類來繼承 Activity

2. 呼叫 onCreate 方法

3. 在 AndroidManifest.xml 中進行註冊

二、如何啟動一個 Activity

1. 生成一個意圖物件(也就是 Intent)

2. 呼叫 Intent 物件的 setClass 方法

3. 呼叫當前 Activity 繼承類中的 startActivity 方法

三、程式碼

第一個 Activity 的 XML 檔:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="啟動一個Activity"/> </RelativeLayout> 

第二個 Activity 的 XML 檔:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="第二個activity" android:gravity="center_horizontal"/> </LinearLayout> 

建立第二個 XML 檔時,要在 AndroidManifest.xml 中註冊:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.android.xiong.moreactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.xiong.moreactivity.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="com.android.xiong.moreactivity.SecondActivity" android:label="secondActivity"> </activity> </application> </manifest> 

啟動第二個 Activity:

 package com.android.xiong.moreactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); OnClick onc=new OnClick(); button1.setOnClickListener(onc); } class OnClick implements OnClickListener{ /** * setClass的第一個方法的參數是一個Context 一個參數是表示你要啟動那個Activity 是一個class對象 * Context是activity的父類 */ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); MainActivity.this.startActivity(intent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved