Andriod learning note Activity, andriod learning note
Activity is the display View container in the andriod system. Each Activity provides a visible area. It is also an interface for user interface interaction with the system, similar to the Tag language in HTML, and similar to the winform interface in net. As a container, there are two aspects: 1) how to call the container, that is, the container's external interface and lifecycle 2) how internal attributes or methods affect internal components or controls.
From the design point of View, Activity is a common container, similar to the View in Flex, the interaction between mouse events, la S, lifecycles, and other interfaces and data transmission are indispensable. In terms of universality, the content and logic must be separated, similar to aspx and mxml. The design pattern will certainly implement the combination pattern and Observer pattern.
Android can contain multiple activities, but only one Activity can be activated at a time. Other activities are not activated. An application can only have one MainActivity entry. Actity contains the following statuses: 1) running status 2) paused status 3) stopped status 4) destroyed status
Activity events include the following:
1) onCreate () 2) onStart () 3) onPause () 4) onResume () 5) onStop () 6) onRestart () 7) onDestroy ()
Activity inherits from ContextThemeWrapper and implements the following interfaces: LayoutInflater. Factory2, Window. Callback, KeyEvent. Callback, View. OnCreateContextMenuListener, and ComponentCallbacks2. Supports layout, event monitoring, and topic replacement.
Create an Activity: 1) Create an Activity class and implement the OnCreate () method:
Public
ClassMainActivity
ExtendsActivity {
PrivateTextView testMessage; // declare the text box
PrivateButton testButton; // declaration Button // declaration event
PrivateButton. OnClickListener calcBMI =
NewButton. OnClickListener (){
@ Override
Public
VoidOnClick (View arg0 ){//
TODOAuto-generated method stub testMessage. setText ("change") ;}}; // click the button to add a listener event.
Private
VoidSetListeners () {testButton. setOnClickListener (calcBMI) ;}@ Override
Protected
VoidOnCreate (Bundle savedInstanceState ){
Super. OnCreate (savedInstanceState); // you can specify setContentView (R. layout.
Activity_main); // Obtain the control
This. FindViews (); // sets the listener
This. SetListeners ();}
Private
VoidFindViews (){
This. TestMessage = (TextView)
This. FindViewById (R. id.
TestMessage);
This. TestButton = (Button)
This. FindViewById (R. id.
Button1); TestMessage. setText ("ss ");}
@ Override
Public
BooleanOnCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu.
Main, Menu );
Return
True;}
}
In the preceding method, the TextView and Button controls are created, and the Button events are set. When you click the Button, the display content of the TextBox is changed.
2) Declare the Activity object. The declared content must be set in AndroidManifest. XML. <application android: allowBackup = "true" android: icon ="
@ Drawable/Ic_launcher "android: label =" @ string/app_name "android: theme =" @ style/AppTheme "> <activity android: name =" com. example. andriodresearch. 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> </application>
3) set the control layout in layout
<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">
// Set TextView
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world" android: id = "@ + id/testMessage"/>
// Set the Button
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/testMessage"
Android: layout_marginTop = "32dp"
Android: layout_toRightOf = "@ + id/testMessage"
Android: text = "change"/>
</RelativeLayout>
Now, a complete Activity instance is complete. The next section will discuss the Activity jump content, and try to explore the problems and benefits from the design ideas.