To create an activity, you must create a subclass of
建立一個activity,必須創建activity的子類別(或是已存在一個子類別)在子類別中,需要執行叫回的方法,此方法是系統呼叫activity在生命周期的不同狀態轉換時,如activity被創建,停止,恢復或毀壞的時候。兩個重要的叫回方法是:Activity
(or an existing subclass of it). In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are:onCreate()
- You must implement this method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call必須執行此方法。當創建activity時系統會呼叫 onCreate() ,當執行時,應該要初始化activity的必要元件。最重要的是,必須呼叫setContentView()以定義版面activity的使用者介面
setContentView()
to define the layout for the activity's user interface. onPause()
- The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).當使用者離開activity的第一個指示時(儘管不總是說activity毀壞)系統呼叫這個方法onPause()。通常應該做改變,應該保持跳出使用者的會話(因為使用者可能回不來)
There are several other lifecycle callback methods that you should use in order to provide a fluid user experience between activities and handle unexpected interuptions that cause your activity to be stopped and even destroyed. All of the lifecycle callback methods are discussed later, in the section aboutManaging the Activity Lifecycle.
有很多其他生命週期叫回方法,應該使用他們以提供 activity 和處理意外中斷之間有流暢的用戶體驗,這些意外會導致 activity 停止甚至毀壞。
Implementing a user interface
The user interface for an activity is provided by a hierarchy of views—objects derived from the
View
class. Each view controls a particular rectangular space within the activity's window and can respond to user interaction. For example, a view might be a button that initiates an action when the user touches it.
Android provides a number of ready-made views that you can use to design and organize your layout. "Widgets" are views that provide a visual (and interactive) elements for the screen, such as a button, text field, checkbox, or just an image. "Layouts" are views derived from
ViewGroup
that provide a unique layout model for its child views, such as a linear layout, a grid layout, or relative layout. You can also subclass the View
and ViewGroup
classes (or existing subclasses) to create your own widgets and layouts and apply them to your activity layout.
The most common way to define a layout using views is with an XML layout file saved in your application resources. This way, you can maintain the design of your user interface separately from the source code that defines the activity's behavior. You can set the layout as the UI for your activity with
setContentView()
, passing the resource ID for the layout. However, you can also create new View
s in your activity code and build a view hierarchy by inserting new View
s into a ViewGroup
, then use that layout by passing the root ViewGroup
to setContentView()
.
For information about creating a user interface, see the User Interface documentation.
以上來源: http://developer.android.com/guide/topics/fundamentals/activities.html
原創翻譯,若有更好的描述歡迎指正批評,謝謝
原創翻譯,若有更好的描述歡迎指正批評,謝謝
沒有留言:
張貼留言