The life cycle of the "1" activity
Activity acts as a component of the performance layer of the Android system because he is in direct contact with the user. An activity has undergone 7 processes in the process of being created and destroyed, oncreat (the first function called after activity creation), OnStart (called when the activity is displayed on the interface), Onrestart (from stop to active call), Onresume (which can be called when interacting with the user), OnPause (called when it enters a paused state), OnStop (called when it enters the stop state), OnDestroy (called before it enters the inactive state).
If we are going to do some work when an activity is loaded, we can reload his oncreat function, like this:
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Basesetcontentview (r.layout.login_activity_layout); Inithttphandler (); Initview ();}
The above Inithttphandler () and Initview () are added so that the system will invoke them when the activity is created.
"2" Initializing interface resources
Android's interface resources are described in XML language, which is well suited for organizing data in key-value form. The interface resource is a bunch of descriptive files, such as the name of the interface element, its size, its location, and so on. We have to do some initialization work in the program, or to get the interface elements, that is, to give these elements some aliases. such as the following:
ImageButton logbutton= (ImageButton) Findviewbyid (R.id.imagebutton1);
We define an object called Logbutton in the program, which is actually a reference to the login button inside the interface resource. This allows us to manipulate the login button through the Logbutton object within the program.
Note that this object is defined after Setcontentview (R.layout.activity_main).
There are also familiar with the Android common interface design basics, because we often have to adapt to different screen size of the mobile phone so in the interface layout in order to balance the aesthetics and adaptability of the general use of the relative layout scheme.
Android Relativelayout Properties
Relative to the given ID control
Android:layout_above the bottom of the control to the control of the given ID;
Android:layout_below Place the bottom of the control under the control of the given ID;
Android:layout_toleftof aligns the right edge of the control with the left edge of the control for the given ID;
Android:layout_torightof aligns the left edge of the control with the right edge of the control for the given ID;
Android:layout_alignbaseline aligns the baseline of the control with the baseline of the given ID;
Android:layout_aligntop aligns the top edge of the control with the top edge of the given ID;
Android:layout_alignbottom aligns the bottom edge of the control with the bottom edge of the given ID;
Android:layout_alignleft aligns the left edge of the control with the left edge of the given ID;
Android:layout_alignright aligns the right edge of the control with the right edge of the given ID;
Relative to Parent component
Android:layout_alignparenttop If true, aligns the top of the control with the top of its parent control;
Android:layout_alignparentbottom If true, aligns the bottom of the control with the bottom of its parent control;
Android:layout_alignparentleft If true, aligns the left part of the control with the left part of its parent control;
Android:layout_alignparentright If true, aligns the right part of the control with the right side of its parent control;
Center
Android:layout_centerhorizontal If True, the control is placed horizontally centered;
Android:layout_centervertical If True, the control is placed vertically centered;
Android:layout_centerinparent If True, the control is placed in the center of the parent control;
Specify moving pixels
The value of the offset on the android:layout_margintop;
Android:layout_marginbottom the value of the offset;
Android:layout_marginleft the value of the left offset;
Android:layout_marginright the value of the right offset;
Example
Android:layout_below = "@id/***"
Android:layout_alignbaseline = "@id/***"
Android:layout_alignparenttop = True
Android:layout_marginleft = "10px"
"3" Register control listener events
In order to listen to the user's actions, we must register the listener function. Common listener functions are Setonclicklistener () Setonfocuschangelistener () Addtextchangedlistener () and touch events.
There are several ways to register a listener, and here is a way to get a uniform processing function. That is, implement the corresponding listener interface, register the control's event, overload the response event inside. For example, the onclick () function of buttom.
Implementing interfaces
Button log_button= (button) Findviewbyid (r.id.button1); Log_button.setonclicklistener (this);
Registering events
@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), "Log", Toast.length_long). Show (); Intent intent=new Intent (mainactivity.this,menuactivity.class); StartActivity (Intent); }
Overloaded Events
Jump between "4" activity
The jump between interfaces is a very important part of the app, and starting another activity from one activity via the intent mechanism is simple and the code is as follows:
Intent intent=new Intent (mainactivity.this,menuactivity.class); StartActivity (Intent);
This is an activity that is explicitly started, as well as an implicit startup, a child-initiated.
"5" Handle message mechanism
The handle messaging mechanism is an important way of interprocess communication in Android, and by handle we can easily send messages to a given target.
Sender:
Message msg = Handler.obtainmessage (); msg.what = Messagecode.resize;msg.arg1 = Change;msg.sendtotarget ();
The receiver needs to overload the Handlemessage function
The Accept processing information function @Override public void handlemessage (Message message) { loginactivity theactivity = Mactivity.get (); if (theactivity = = null) return;
Storage of "6" data
There are 5 ways to store data on Android, namely:
1 Storing data using sharedpreferences
2 File Store data
3 SQLite Database stores data
4 Storing data using ContentProvider
5 Networked Storage data
Among them, Sharedpreferences is a lightweight storage class on the Android platform, mainly to save some common configuration such as window state, usually in the activity Reload window state onsaveinstancestate save is generally done using sharedpreferences, which provides the Android platform General Long shaping, int shaping, string-string saving. What is the way it is handled? Sharedpreferences is similar to the previous INI configuration file on Windows systems, but it is divided into multiple permissions, can be globally shared access, android123 hints are ultimately stored in XML, overall efficiency is not particularly high, It's a lot better for regular lightweight than SQLite, if you really don't have enough storage to consider your own definition file format. XML processing is Dalvik through the native XML parser, such as the Xmlpull method, which is better for memory resource consumption. Its essence is to store Key-value key-value pairs of data based on XML files, usually to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object.
The steps to implement Sharedpreferences storage are as follows:
1 Context gets Sharedpreferences object
2 the edit () method gets the editor object.
The 3 Editor object stores key-value key-value pairs of data.
The 4 commit () method submits the data.
public class Mainactivity extends Activity { @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Gets the Sharedpreferences object Context ctx = mainactivity.this; Sharedpreferences sp = ctx.getsharedpreferences ("sp", mode_private); Deposit Data Editor editor = Sp.edit (); Editor.putstring ("String_key", "STRING"); Editor.putint ("Int_key", 0); Editor.putboolean ("Boolean_key", true); Editor.commit (); Returns the value of String_key log.d ("SP", Sp.getstring ("String_key", "none")); If Not_exist does not exist, the return value is "None" log.d ("SP", Sp.getstring ("Not_exist", "none"));} }