Interface Composition in Android
The screen is a simplified window, which is often used in Android ". Screen and screen content are the main content of UI construction in Android. These two aspects include:
Screen: Activity (Android. App. activity );
Content on the screen: View (Android. View. View ).
The program structure of the screen and content in Android is 2-1.
As shown in Figure 2-1, the activity in the Android. app package and the view in the Android. View package are used to represent the base class of a screen and a view respectively.
The basic construction method of the android user interface is divided into the following steps:
Inherit the default activity to implement your own screen;
Use the owner of the view as the content on the screen;
Responds to events in the screen and content.
Screen (activity)
Android. App. activity is an activity in Android. It is a component used to represent the UI in Android. The most important content in an activity is a view. In addition, this class also contains the title bar, menu, and dialog box ancillary functions.
The activity lifecycle is 2-2.
When implementing your own activities in an application, you usually need to inherit an activity class and mainly implement the following protected methods in the activity:
Protected void oncreate (bundlesavedinstancestate) // called at creation
Protected void onstart () // called at the start
Protected void onresume () // called upon recovery
Protected void onpause () // called when it is stopped
Protected void onstop () // called when the instance is completely stopped
Protected void onrestart () // call when you restart
Protected void ondestroy () // called upon destruction
These methods starting with on are used to specify the behavior of an active state machine at the time of transition and will be called at different stages of an active life cycle.
In an activity, the following APIs are provided to set the content:
Public void setcontentview (INT layoutresid) // set it as a layout File
Public void setcontentview (view) // you can set it as a view class.
Public void setcontentview (view, viewgroup. layoutparams Params)
Setcontentview () is used to set a view to an activity. It is usually called in the oncreate () stage of the implemented activity.
On-screen content (view)
Android. View. view indicates the visible View content, which is visible on the interface and can receive events. The class used in the program is generally the successor of the view. The successors of various views can be described in the layout file.
The main identifier of a view is its ID, which is the method used to find the sub-view.
Public final view findviewbyid (int id)
View events are mainly obtained by the listener. In the View class, there are Seton {XXX} listener () series of methods. The content is as follows:
Public void setonclicklistener (view. onclicklistener L );
Public void setonlongclicklistener (view. onlongclicklistener L );
Public void setonfocuschangelistener (view. onfocuschangelistener L );
Public void setonkeylistener (view. onkeylistener L );
Public void setontouchlistener (view. ontouchlistener L );
Public void setoncreatecontextmenulistener (view. oncreatecontextmenulistener L)
The parameters of these methods are the interfaces in the Android. View package, as shown in Table 2-1.
Table 2-1 event processing interface
|
Listener Function
|
Listener
|
Methods In the interface
|
|
Click
|
Onclicklistener
|
Void onclick (view V)
|
|
Long press
|
Onlongclicklistener
|
Boolean onlongclick (view V)
|
|
Focus changes
|
Onfocuschangelistener
|
Void onfocuschange (view V, Boolean hasfocus)
|
|
Buttons
|
Onkeylistener
|
Boolean onkey (view V, int keycode, keyevent event)
|
|
Touch
|
Ontouchlistener
|
Boolean ontouch (view V, motionevent event)
|
|
Context Menu
|
Oncreatecontextmenulistener
|
Void oncreatecontextmenu (contextmenu menu, view V,
Contextmenu. contextmenuinfo menuinfo)
|
These interfaces are usually implemented by user programs. In the methods to be implemented, view is the first parameter. When these methods are called, the first parameter is usually the view of the event.
Combination of the two
View and its successors can usually be set to activity and used as content. The setcontentview () method of the activity is usually used.
For example, you can use the following method to set a layout file to an activity.
Setcontentview (R. layout. mylayout );
The layout file can be converted into a view class. This is the layoutinflater class in the Android. View package. In this case, we mainly use the following methods in the class:
Public View inflate (INT resource, viewgroup root)
Public View inflate (INT resource, viewgroup root, booleanattachtoroot)
Public View inflate (xmlpullparser parser, viewgroup root)
Public View inflate (xmlpullparser parser, viewgroup root, booleanattachtoroot)
The layoutinflater function is to convert the layout File ID or XML file into a view-type content.
Make the following calls in an activity to set the layout file to it:
Layoutinflater factory = layoutinflater. From (this );
Final view mylayout = (View) Factory. Inflate (R. layout. mylayout, null );
Setcontentview (mylayout );
This implementation method is equivalent to setting the layout file to the activity directly, but the two setcontentview () parameters are different. The former uses an integer parameter to indicate the layout File ID, the latter uses a view type. After parsing, the layout file in XML format is actually a multi-level view.
Tip: setcontentview () in activity is generally used to set the layout file. There is no need to split it into two steps: parsing and setting. Layoutinflater can convert a layout file to a view when you need parameters of the view type.
-- This text excerpt is from Android classic application development.
Book details: http://blog.csdn.net/broadview2006/article/details/7276196