Getting started with Android and proficient in Interface Programming start with the Button

Source: Internet
Author: User

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.

 
 
  1. public class HelloActivity extends Activity {  
  2.    /** Called when the activity is first created. */  
  3.    @Override  
  4.    public void onCreate(Bundle savedInstanceState) {  
  5.        super.onCreate(savedInstanceState);  
  6.        TextView tv = new TextView(this);  
  7.        tv.setText("Hello, World!");  
  8.        this.setContentView(tv);  
  9.         
  10.    }  

Layout is not used here, which is a separate component method. You can also change it:

 
 
  1. super.onCreate(savedInstanceState);  
  2. Button btn = new Button(this);  
  3. btn.setText("TestButton");  
  4. 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.

 
 
  1. super.onCreate(savedInstanceState);  
  2. Button btn = new Button(this);  
  3. btn.setText("TestButton");  
  4. Button btn2 = new Button(this);  
  5. btn2.setText("TestButton2");  
  6. LinearLayout layout = new LinearLayout(this);  
  7. layout.setOrientation(LinearLayout.VERTICAL);  
  8. layout.addView(btn);  
  9. layout.addView(btn2);  
  10. 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.

 
 
  1. import android.app.Activity; import android.os.Bundle;  
  2. import android.view.View;  
  3. import android.view.View.OnClickListener;  
  4. import android.widget.Button;  
  5. import android.widget.LinearLayout;  
  6.  
  7. public class HelloActivity extends Activity implements OnClickListener {  
  8.   Button btn = null;  
  9.   Button btn2 = null;  
  10.     public void onClick(View v) {      
  11.   if (v ==  btn)  
  12.   {  
  13.       this.setTitle("You Clicked Button1");        
  14.   }  
  15.   if (v ==  btn2)  
  16.   {  
  17.       this.setTitle("You Clicked Button2");  
  18.   }    
  19.  }   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         btn = new Button(this);  
  24.         btn2 = new Button(this);  
  25.         btn.setText("TestButton1");          
  26.         btn2.setText("TestButton2");  
  27.         btn.setOnClickListener(this);  
  28.         btn2.setOnClickListener(this);  
  29.         LinearLayout layout = new LinearLayout(this);  
  30.         layout.setOrientation(LinearLayout.VERTICAL);  
  31.         layout.addView(btn);  
  32.         layout.addView(btn2);  
  33.         this.setContentView(layout);         
  34.     }  

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.