AndroidThere are two basic methods for 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 to 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 management 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. For the value of the View parameter, determine the View (Button ). 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.
Activity page Switching Effect
Loading modes of four Android activities
Android development: Passing values between activities
Android app Activity parameters and redirection
How to install and uninstall programs in the Android Simulator