设计开发分发

Android培训API指南参考工具

应用程序组件

Activities

AnActivityisanapplicationcomponentthatprovidesascreenwithwhichuserscaninteractinordertodosomething,suchasdialthephone,takeaphoto,sendanemail,orviewamap.Eachactivityisgivenawindowinwhichtodrawitsuserinterface.Thewindowtypicallyfillsthescreen,butmaybesmallerthanthescreenandfloatontopofotherwindows.

一个Activity是一个应用程序组件,提供一个屏幕,用户可以交互来做一些事情;比如:打电话,拍照,发电子邮件,或者查看地图。每一个活动都被给予了一个窗口来描绘它的用户界面。窗口通常会占据整个屏幕,但也可能小于屏幕,或飘于其它窗口的上面。

Anapplicationusuallyconsistsofmultipleactivitiesthatarelooselyboundtoeachother.Typically,oneactivityinanapplicationisspecifiedasthe"main"activity,whichispresentedtotheuserwhenlaunchingtheapplicationforthefirsttime.Eachactivitycanthenstartanotheractivityinordertoperformdifferentactions.Eachtimeanewactivitystarts,thepreviousactivityisstopped,butthesystempreservestheactivityinastack(the"backstack").Whenanewactivitystarts,itispushedontothebackstackandtakesuserfocus.Thebackstackabidestothebasic"lastin,firstout"stackmechanism,so,whentheuserisdonewiththecurrentactivityandpressestheBackbutton,itispoppedfromthestack(anddestroyed)andthepreviousactivityresumes.(ThebackstackisdiscussedmoreintheTasksandBackStackdocument.)

一个应用程序通常有许多活动组成以至于它们是彼此宽松的限制。通常,一个活动应用程序被指定为主要活动,这是提供给用户当第一次启动应用程序的时候。每一个活动就可以启动另外一个活动来执行不同的操作。每次一个新的活动开始,前面的活动停止,但系统保存一个活动在一个堆中(返回堆栈)。当一个新的活动开始,它被推到返回堆栈和需要用户的焦点。返回堆栈的基本守则是:后进先出堆栈机制,因此,当用户完成当前活动,并按下返回按钮时,它从堆栈中弹出(和摧毁)和重新获得前面活动的动作。(返回堆栈在后面的返回堆栈文档中谈论的更多)

Whenanactivityisstoppedbecauseanewactivitystarts,itisnotifiedofthischangeinstatethroughtheactivity'slifecyclecallbackmethods.Thereareseveralcallbackmethodsthatanactivitymightreceive,duetoachangeinitsstate—whetherthesystemiscreatingit,stoppingit,resumingit,ordestroyingit—andeachcallbackprovidesyoutheopportunitytoperformspecificworkthat'sappropriatetothatstatechange.Forinstance,whenstopped,youractivityshouldreleaseanylargeobjects,suchasnetworkordatabaseconnections.Whentheactivityresumes,youcanreacquirethenecessaryresourcesandresumeactionsthatwereinterrupted.Thesestatetransitionsareallpartoftheactivitylifecycle.

当一个活动停止由于一个新的活动开始,它被告知这个改变状态通过活动的生命周期回调方法。有几个回调方法,一个活动可以收到几个,由于一个状态的改变,是否系统创建它,阻止他,恢复它,或摧毁它----和每一个回调方法都提供你机会去执行特定工作的恰当状态改变。例如,当你活动停止的时候,你的活动应该释放所有的对象,如:网络,数据库连接。当活动恢复的时候,你可以重拾哪些必要的资源,和恢复动作以至于被打断。这些状态的改变都是所有部分的生命周期。

Therestofthisdocumentdiscussesthebasicsofhowtobuildanduseanactivity,includingacompletediscussionofhowtheactivitylifecycleworks,soyoucanproperlymanagethetransitionbetweenvariousactivitystates.

本文的其余部分讨论了基本的如何构建和使用一个Activity,包含一个完整的生命的周期的讨论活动如何工作,所以你可以适当的管理各种活动状态之间的转换。

CreatinganActivity

Tocreateanactivity,youmustcreateasubclassofActivity(oranexistingsubclassofit).Inyoursubclass,youneedtoimplementcallbackmethodsthatthesystemcallswhentheactivitytransitionsbetweenvariousstatesofitslifecycle,suchaswhentheactivityisbeingcreated,stopped,resumed,ordestroyed.Thetwomostimportantcallbackmethodsare:

创建一个活动,你必须创建一个子类Activity(或者一个现有的子类)。在你的子类中,你需要实现回调方法以至于系统回调,当活动转换在它们生命周期的各种状态,如当一个活动被创建,停止,恢复,销毁。最重要的两个回调方法是:

onCreate()

Youmustimplementthismethod.Thesystemcallsthiswhencreatingyouractivity.Withinyourimplementation,youshouldinitializetheessentialcomponentsofyouractivity.Mostimportantly,thisiswhereyoumustcallsetContentView()todefinethelayoutfortheactivity'suserinterface.

你必须实现这个方法,系统回调这个方法当创建你的活动的时候。在你实现期间,你应该初始化你的活动的基本必须的组件。最重要的是;你必须调用SetContentView()定义布局活动的用户界面。

onPause()

Thesystemcallsthismethodasthefirstindicationthattheuserisleavingyouractivity(thoughitdoesnotalwaysmeantheactivityisbeingdestroyed).Thisisusuallywhereyoushouldcommitanychangesthatshouldbepersistedbeyondthecurrentusersession(becausetheusermightnotcomeback).

系统调用这个方法作为第一个迹象表明用户离开你的活动(尽管它并不总意味着活动总被破坏)。这应该是你提交任何变化应坚持超越当前用户的会话(因为用户可能不回来了)

Thereareseveralotherlifecyclecallbackmethodsthatyoushoulduseinordertoprovideafluiduserexperiencebetweenactivitiesandhandleunexpectedinteruptionsthatcauseyouractivitytobestoppedandevendestroyed.Allofthelifecyclecallbackmethodsarediscussedlater,inthesectionaboutManagingtheActivityLifecycle.

还有其他几个生命周期回调方法,你应该使用,以提供一个流体的用户体验活动和处理意外的中断以至于引起你的活动被停止和甚至被破坏。所有的生命周期回调方法进行讨论之后,在这一节关于管理生命周期的活动。

Implementingauserinterface

Theuserinterfaceforanactivityisprovidedbyahierarchyofviews—objectsderivedfromtheViewclass.Eachviewcontrolsaparticularrectangularspacewithintheactivity'swindowandcanrespondtouserinteraction.Forexample,aviewmightbeabuttonthatinitiatesanactionwhentheusertouchesit.

用户界面为一个活动,是提供一个层次的views-objects源自视图类。每个视图控制一个特定的矩形空间在该活动的窗口,可以响应用户的交互。例如,一个视图可能是一个按钮,启动一个行动当用户触摸它。

Androidprovidesanumberofready-madeviewsthatyoucanusetodesignandorganizeyourlayout."Widgets"areviewsthatprovideavisual(andinteractive)elementsforthescreen,suchasabutton,textfield,checkbox,orjustanimage."Layouts"areviewsderivedfromViewGroupthatprovideauniquelayoutmodelforitschildviews,suchasalinearlayout,agridlayout,orrelativelayout.YoucanalsosubclasstheViewandViewGroupclasses(orexistingsubclasses)tocreateyourownwidgetsandlayoutsandapplythemtoyouractivitylayout.

Android提供了很多现成的视图,您可以使用它来设计和组织你的布局。“插件”是视图,这些视图提供视觉(和互动)屏幕元素,比如一个按钮,文本域,复选框,或者只是一个图像。“布局”是源自ViewGroup视图,提供一个独特的布局模式子视图,如线性布局,网格布局,或者相对的布局。你也可以细分视图和ViewGroup类(或现有的子类)来创建自己的小部件和布局,并将它们应用到你的activity布局

ThemostcommonwaytodefinealayoutusingviewsiswithanXMLlayoutfilesavedinyourapplicationresources.Thisway,youcanmaintainthedesignofyouruserinterfaceseparatelyfromthesourcecodethatdefinestheactivity'sbehavior.YoucansetthelayoutastheUIforyouractivitywithsetContentView(),passingtheresourceIDforthelayout.However,youcanalsocreatenewViewsinyouractivitycodeandbuildaviewhierarchybyinsertingnewViewsintoaViewGroup,thenusethatlayoutbypassingtherootViewGrouptosetContentView().

最常见的方式来定义一个布局使用视图是使用XML布局文件保存在您的应用程序资源。通过这种方式,您可以维护用户界面的设计,分别从源代码中,定义了活动的行为。你可以设置的布局为你的活动与UIsetContentView(),通过对布局的资源ID。然而,你也可以在你的活动创建新的视图代码并构建一个视图层次插入新视图ViewGroup,然后用这个布局通过根ViewGroup到setContentView()。

Forinformationaboutcreatingauserinterface,seetheUserInterfacedocumentation.

有关信息创建一个用户界面,看用户界面文档。

Declaringtheactivityinthemanifest

Youmustdeclareyouractivityinthemanifestfileinorderforittobeaccessibletothesystem.Todeclareyouractivity,openyourmanifestfileandaddan<activity>elementasachildofthe<application>element.Forexample:

你必须声明你的activitymanifest文件中以访问系统。声明你的activity,打开你的manifest文件并添加一个<activity>元素的子元素<application>。例如:

Thereareseveralotherattributesthatyoucanincludeinthiselement,todefinepropertiessuchasthelabelfortheactivity,aniconfortheactivity,orathemetostyletheactivity'sUI.Theandroid:nameattributeistheonlyrequiredattribute—itspecifiestheclassnameoftheactivity.Onceyoupublishyourapplication,youshouldnotchangethisname,becauseifyoudo,youmightbreaksomefunctionality,suchasapplicationshortcuts(readtheblogpost,ThingsThatCannotChange).

还有其他几个属性,您可以包括在这个元素,定义属性如标签活动,该活动的一个图标,或一个主题风格活动的UI。android:name属性是惟一需要attribute-it指定类名的活动。一旦你发布你的应用程序,您不应该更改这个名称,因为如果你这样做了,你可能会破坏某些功能,比如应用程序快捷方式(读这篇博客文章,一些不能改变)。

Seethe<activity>elementreferenceformoreinformationaboutdeclaringyouractivityinthemanifest

看到<activity>元素引用信息声明你的activity在清单中

Usingintentfilters

上图:Theactivitylifecycle.(重要)

主要七种方法:onCreate()

onStart()

onRestart()

onResume()

onPause()

onStop()

onDerstory()

更多相关文章

  1. Android 仿抖音上下滑动布局
  2. Android UI布局经验总结
  3. Android网格布局实现--GridView
  4. Android的xml布局文件代码讲解(TextView控件)
  5. 可动态布局的Android抽屉之完整篇
  6. 【从头学android】第二个程序同一个Activity中,切换布局时监听器
  7. ANDROID L——Material Design详解(视图和阴影)
  8. Android23-视图坐标系以及MotionEvent事件
  9. [Android]ButterKnife-无尽之刃-绑定视图控件和事件的快速开发工

随机推荐

  1. android 4.4 沉浸式状态栏实现
  2. Android(安卓)网络编程 Socket Http
  3. Android(安卓)多语言与国际化
  4. 2019 Android开发趋势及必备技术点!
  5. android:强大的图片下载和缓存库Picasso
  6. android完整权限
  7. android ble蓝牙开发略解
  8. Android利用ContentResolver查询的三种方
  9. Android(安卓)Logcat和Debug的使用
  10. Android引用第三方jar的问题