通過實(shí)例簡(jiǎn)單講解Android App中的Activity組件
Activity是Android應(yīng)用中,最直接與用戶接觸的組件,它負(fù)責(zé)加載View組件,使其展現(xiàn)給用戶,并保持與用戶的交互。所有的Activity組件均需要繼承Activity類,這是一個(gè)Content的間接子類,包裝了一些Activity的基本特性。
View組件是所有UI組件、容器組件的基類,也就是說,它可以是一個(gè)布局容器,也可以是一個(gè)布局容器內(nèi)的基本UI組件。View組件一般通過XML布局資源文件定義,同時(shí)Android系統(tǒng)也對(duì)這些View組件提供了對(duì)應(yīng)的實(shí)現(xiàn)類。如果需要通過某個(gè)Activity把指定的View組件顯示出來,調(diào)用Activity的setContentView()方法即可,它具有多個(gè)重載方法,可以傳遞一個(gè)XML資源ID或者View對(duì)象。
例如
LinearLayout layout=new LinearLayout(this); setContentView(layout);
或者
setContentView(R.layout.main);
Activity為Android應(yīng)用提供了一個(gè)用戶界面,當(dāng)一個(gè)Ac-tivity被開啟之后,它具有自己的生命周期。Activity類也對(duì)這些生命周期提供了對(duì)應(yīng)的方法,如果需要對(duì)Activity各個(gè)不同的生命周期做出響應(yīng),可以重寫這些生命周期方法實(shí)現(xiàn)。對(duì)于大多數(shù)商業(yè)應(yīng)用而言,整個(gè)系統(tǒng)中包含了多個(gè)Activity,在應(yīng)用中逐步導(dǎo)航跳轉(zhuǎn)開啟這些Activity之后,會(huì)形成Activity的回退棧,當(dāng)前顯示并獲得焦點(diǎn)的Activity位于這個(gè)回退棧的棧頂。
實(shí)例
一、如何在定義多個(gè)Activity
1. 定義一個(gè)類來繼承Activty
2.調(diào)用Oncreate方法
3.在Anroidmianifest.xml中進(jìn)行注冊(cè)
二、如何啟動(dòng)一個(gè)Activity
1.生成一個(gè)意圖對(duì)象(也就是Intent)
2.調(diào)用Intent對(duì)象的SetClass方法
3.調(diào)用當(dāng)前Activity繼承類中的startActiviyt的方法
三、代碼
一個(gè)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="啟動(dòng)一個(gè)Activity"/> </RelativeLayout>
第二個(gè)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="第二個(gè)activity" android:gravity="center_horizontal"/> </LinearLayout>
在創(chuàng)建第二個(gè)xml文件是在Androidmianifest.xml文件中注冊(cè)
<?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>
啟動(dòng)第二個(gè)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的第一個(gè)方法的參數(shù)是一個(gè)Context 一個(gè)參數(shù)是表示你要啟動(dòng)那個(gè)Activity 是一個(gè)class對(duì)象 * 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; } }
相關(guān)文章
Android Toolbar自定義標(biāo)題標(biāo)題居中的實(shí)例代碼
這篇文章主要介紹了Android Toolbar自定義標(biāo)題 標(biāo)題居中的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例
本篇文章主要介紹了Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07Android 實(shí)現(xiàn)滑動(dòng)方法總結(jié)
這篇文章主要介紹了Android 實(shí)現(xiàn)滑動(dòng)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07Android自定義帶動(dòng)畫效果的圓形ProgressBar
這篇文章主要為大家詳細(xì)介紹了Android自定義帶動(dòng)畫效果的圓形ProgressBar,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android 中SP與DP的區(qū)別實(shí)例詳解
這篇文章主要介紹了Android 中SP與DP的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
本篇文章主要介紹了android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04