通過執行個體簡單講解Android App中的Activity組件_Android

來源:互聯網
上載者:User

Activity是Android應用中,最直接與使用者接觸的組件,它負責載入View組件,使其展現給使用者,並保持與使用者的互動。所有的Activity組件均需要繼承Activity類,這是一個Content的間接子類,封裝了一些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應用提供了一個使用者介面,當一個Ac-tivity被開啟之後,它具有自己的生命週期。Activity類也對這些生命週期提供了對應的方法,如果需要對Activity各個不同的生命週期做出響應,可以重寫這些生命週期方法實現。對於大多數商業應用而言,整個系統中包含了多個Activity,在應用中逐步導航跳轉開啟這些Activity之後,會形成Activity的回退棧,當前顯示並獲得焦點的Activity位於這個回退棧的棧頂。

執行個體

一、如何在定義多個Activity
1. 定義一個類來繼承Activty
2.調用Oncreate方法
3.在Anroidmianifest.xml中進行註冊

二、如何啟動一個Activity
1.產生一個意圖對象(也就是Intent)
2.調用Intent對象的SetClass方法
3.調用當前Activity繼承類中的startActiviyt的方法

三、代碼
一個Activity的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=".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="http://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檔案是在Androidmianifest.xml檔案中註冊

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://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;   } } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.