There are two basic methods for Android interface programming. One is to dynamically create components in the Code and combine these components with layout to form a complex interface display. One is to write layout in a graphical way. The layout is saved in an XML file and compiled into resources. It is loaded by the activity in the Program (setcontentview ()), then, use the findviewbyid method to obtain the reference of each interface component. Most people prefer the most intuitive method, that is, the dynamic generation method in code. Let's start with this. We will talk about visual programming and resource layout later.
1. Layout)
Each interface component is a child class of the view and can occupy a single screen. However, a really useful interface is a combination of these components. In Android, various layout components are used for layout management, this is basically the same as some AWT and swing interfaces in the traditional j2se.
2. A separate interface element:
In the Hello world example, we have mentioned such a piece of code. In activity.
Public class helloactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Textview TV = new textview (this );
TV. settext ("Hello, world! ");
This. setcontentview (TV );
}
}
Layout is not used here, which is a separate component method. You can also change it:
Super. oncreate (savedinstancestate );
Button BTN = new button (this );
BTN. settext ("testbutton ");
This. setcontentview (BTN );
Compile and run a full screen button. Of course, this is not the practical interface you want. We will use layout to layout it.
Super. oncreate (savedinstancestate );
Button BTN = new button (this );
BTN. settext ("testbutton ");
Button btn2 = new button (this );
Btn2.settext ("testbutton2 ");
Linearlayout layout = new linearlayout (this );
Layout. setorientation (linearlayout. Vertical );
Layout. addview (BTN );
Layout. addview (btn2 );
This. setcontentview (layout );
After compiling and running, you can see two buttons in the upper and lower order. Of course, for the layout manager, AWT is used on PC and swing is no stranger. I will not go into details here. How can we respond to the event? It may be difficult to guess that in AWT, in the mobile phone's j2's, listener is used to handle event responses, and Android is not vulgar. This is the same as the observer in BlackBerry and Symbian. Both use the observer mode of the design mode. The following is an example of Event Response.
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. linearlayout;
Public class helloactivity extends activity implements onclicklistener {
Button BTN = NULL;
Button btn2 = NULL;
Public void onclick (view v ){
If (V = BTN)
{
This. settitle ("You clicked button1 ");
}
If (V = btn2)
{
This. settitle ("You clicked button2 ");
}
}
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
BTN = new button (this );
Btn2 = new button (this );
BTN. settext ("testbutton1 ");
Btn2.settext ("testbutton2 ");
BTN. setonclicklistener (this );
Btn2.setonclicklistener (this );
Linearlayout layout = new linearlayout (this );
Layout. setorientation (linearlayout. Vertical );
Layout. addview (BTN );
Layout. addview (btn2 );
This. setcontentview (layout );
}
}
Steps:
1. generate two buttons and configure the Click Event listener as helloactivity. This class implements the onclicklistener interface.
2. Put the button into the layout and display two buttons according to the layout
3. Press a button to generate a click event and call the onclick interface function of helloactivity.
4. Determine which view (button) is the value of the view parameter ). Rewrite the tititile content of the activity. Note: Do not compare the view. GETID (). By default, the ID value of each component is-1 unless the id value is set manually. when visualized programming is used, an ID value is automatically generated for the component.