Basic components of Android apps

Source: Internet
Author: User

one of the simplest applications consists of an activity and a layout. First,activity is a specific instance of the activity class that manages the user's interaction with the information screen. The functionality of the application is implemented by writing an activity sub-class. Quizactivity.java
public class Quizactivity extends Activity {
......
}
The Java class file consists of two activity methods: The Java class file contains two activity methods:
OnCreate (Bundle) and Oncreateoptionsmenu (Menu).
once an instance of the activity subclass is created, the OnCreate (Bundle) method is called. Once the activity is created, it needs to be taken and managed by its own user interface.
Gets the activity's user interface, which invokes the following activity methods:
Public void Setcontentview (int layoutresid)by passing in the resource ID parameter of the layout, the method generates a view of the specified layout and places it on the screen. Layout view explodedThe layout file contains components that are then instantiated with their respective property definitions. The user interface for getting the app is: Setcontentview (r.layout.main);
Two
A layout defines a series of user-interface objects and where they appear on the screen. The definition of the composition layout is saved in the XML file, the file name is Main.xml,
Under the res/layout/directory. All of the elements in the file are defined as a component, and some attributes are defined, if the resource ID needs to be defined for use in programming.
All resource IDs are in R.java, generated automatically at compile time, and can be viewed.
Android:id= "@+id/true_button"
The resource ID is R.id.true_button.
The ID of the layout resource is: R.layout.main
The view component is included in the view layer structure. (1) Component properties:1. Android:layout_width and android:layout_height require android:layout_width and android:layout_height attributes for almost every type of component. they are usually set to one of the following two property values. Match_parent: The view is the same size as its parent view. Wrap_content: The view will automatically resize according to its content. 2. The Android:orientation property Android:orientation property determines whether the component is placed horizontally or vertically. 3. The Android:text property TextView has a Android:text property with the button component. This property specifies the text content displayed by the Component Android:text property value is not a string literal, but a reference to a string resource (string resources). The string resource is contained in a separate XML file named Strings, in Res/values. (2) The string value of all components to be displayed is defined in the string resource String.xml. <string name = "True_button" >True</string>The resource ID is R.string.true_button. (3) component Referencethe generated components can be referenced by the following activity methods:Public View Findviewbyid (int id)If the True button is displayed:private Button Mtruebutton;Mturebutton = (Button) Findviewbyid (R.id.true_button);(4) Setting up listenersAn object created to respond to an event is called a listener, such as listening for a user's button "click" Event, and the listener implements the View.onclicklistener interface. The onclick (View) method is required to implement the listener interface. All listeners are implemented as anonymous internal classes.
= (Button) Findviewbyid (r.id. True_button);
Mtruebutton. Setonclicklistener (View.onclicklistener () {
public void OnClick(View v) {
Toast.Maketext (Quizactivi Ty.this,r.string.incorrect_toast, Toast.length_short). Show () ;
    }
});
a short pop-up message that the toast uses to inform the user.   

Basic components of Android apps

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.