Android uses Hello World for Project Structure Analysis

Source: Internet
Author: User

First, create a class HelloWorldActivity:
Public class HelloWordActivity extends Activity {

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

SetTitle ("Hello World ");

}

In this class, we can see that the class we write inherits the Activity class, which is very common in android. An Activity is equivalent to a screen on a mobile phone screen. If you compare a mobile phone to a browser, the Activity is equivalent to a Web page. In the Activity, you can view some controls and process them accordingly. An android program usually consists of multiple activities, which can jump between different activities; similar to webpage jump. In addition, redirection between activities can return values. For example, jump from Activity1 to Activity2. After Activity2 ends, a value may be returned to Activity1. the lifecycle of the Activity is maintained by the android system. This will be detailed in future articles.

When implementing your own Activity, you generally need to overload the onCreate method to add controls and process related functions of controls. SetContentView (R. layout. main); is to implement the Add layout (view) of the control ). The R class is responsible for finding the view. R. layout. main points to the configuration file res/layout/main. xml. In this configuration file, you can add controls and layout controls. The following describes the structure of the android project to help you understand this problem.

As shown in:


 


Src is written in the following code. At the same time, we can see that there is also a gen directory, under which there is a R. java file. The R file is automatically generated by the ADT, And the programmer does not need to modify it. The R file is responsible for calling non-code Resources in the application. Each resource file in the R file corresponds to an integer. As follows:

 

Public final class R {

Public static final class attr {

}

Public static final class drawable {

Public static final int icon = 0x7f020000;

}

Public static final class id {

Public static final int button1 = 0x7f050006;

Public static final int button2 = 0x7f050007;

Public static final int lineLayout1 = 0x7f050003;

Public static final int linelayout1 = 0x7f050002;

Public static final int text1 = 0x7f050000;

Public static final int text2 = 0x7f050001;

Public static final int textview1 = 0x7f050004;

Public static final int textview2 = 0x7f050005;

}

Public static final class layout {

Public static final int frame_layout = 0x7f030000;

Public static final int main = 0x7f030001;

}

Public static final class string {

Public static final int app_name = 0x7f040001;

Public static final int hello = 0x7f040000;

}

}

Note that the android system also has an R. java file, which is under the android package. When importing the R of this system (import android. R;), we need to access the R. java of the project by using the package name. For example:

SetContentView (org. terry. R. layout. main );

The Res directory corresponds to this resource file, including the images and xml files used by the project. That is, the resource Directory, which stores resource files and manages resource files in a unified manner, is also a major feature of the Android system. Note that the main. xml files (main. xml file, used for the layout of the main Activity. If you need to set the layout of other activities, you can add other xml files ). The content of this file is related to the layout and design of the user interface. setContentView (R. layout. main) is to set the layout of the user interface. This is similar to the html tag language in a webpage.

Under the project and directory, you can also see an AndroidManifest. xml file, which is required for every android project. This file is automatically generated when you create a project. This file includes program components, implemented functions, processed data, and requested resources to the Android operating system. If you know Java web development, you can use web. xml to compare this file.

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "org. terry"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "11"/>

 

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". HelloWordActivity"

Android: label = "@ string/app_name">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

<Activity android: name = ". FrameLayoutActivity"

Android: label = "@ string/app_name">

</Activity>

</Application>

</Manifest> where Manifest is the root node, versionCode and versionName indicate the version of the application. It can contain 0 or 1 application element, and application can contain multiple activity components, the specific content will be explained in detail in the next course. The implementation of the activity contained in the program must be defined in this file. For example, if the FrameLayoutActivity added in the application is not added, when the program calls this class, the error of the class cannot be found.

Author: "algorithm java swing"
 

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.