The first Android Program-understanding the file structure

Source: Internet
Author: User

Preliminary understanding of the Activity: Like a window, it can display information, and like a container, it can accommodate functional spaces, such as buttons. From a program perspective, it is like a class, it can be associated with other classes (activities.

Key points for creating an Activity:

An Activity is a class. The class name can start at will, but it must inherit the parent class of Activity.
OnCreate () method needs to be rewritten
Every Activity should be configured in the AndroidManifest. xml file.
Add necessary controls for Activity
Preview The overall file code:

View Code
// MyActivity. java file package geeker. myActivity; import android. app. activity; import android. OS. bundle; import android. widget. button; import android. widget. textView; public class MyActivity extends Activity {// declaration of member variables private TextView myTextView = null; private Button myButton = null; // override OnCreate method, public void onCreate (Bundle savedInstanceState) will be automatically generated {// call the parent class method. This code will automatically generate super. onCreate (savedInstanceStat E); // call the layout file setContentView (R. layout. main); // use the findViewById () method to obtain the control added to the layout file. // However, when adding a control to the layout file, you must define the ID number, for example, android: id = "@ + id/myTextView" myTextView = (TextView) findViewById (R. id. myTextView); myButton = (Button) findViewById (R. id. myButton); // display the text myTextView to the control. setText ("This is my first Activity! "); MyButton. setText (" my first button ") ;}// main. xml file <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: id = "@ + id/myTextView" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/myButton" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </ LinearLayout> // R. Jar FILE. Do not change the/* AUTO-generated file. do not modify. ** This class was automatically generated by the * aapt tool from the resource data it found. it * shoshould not be modified by hand. */package geeker. myActivity; 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 myButton = 0x7f050001; public static final int myTextView = 0x7f050000;} public static final class layout {public static final int main = 0x7f030000 ;} public static final class string {public static final int app_name = 0x7f040001; public static final int hello = 0x7f040000 ;}}
In fact, the process of adding a Button is to understand the relationship between each file:

1. Open the main. xml file and add a button layout.

<Button android: id = "@ + id/myButton" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/>
2. After the previous step is completed, you can see a Button after compiling and running. However, I want to add text on the Button to demonstrate the function of the Button. The program in java is:

Button bt = new Button (); bt. setText ("my first button ");
In the Android program, how does one obtain the control just added in main. xml in the. java source file?

For this purpose. android: id = "@ + id/myButton" is added to the xml file. A class named id is added to the java file, and the id of the control appears in this class. java file.

In fact, if you do not add a sentence, the control will not be in the R. java files generate id numbers, because only files added to the res directory are automatically added to the R. java generates an ID number, while adding a control is only modifying it in a resource file, so it does not automatically generate an ID number.

Let's take a look at the ID code automatically generated in the R. java file:

Public static final class id {public static final int myButton = 0x7f050001; public static final int myTextView = 0x7f050000 ;}
In the. java file, you can use the getViewById () method to obtain the control.

After obtaining the control, you can perform related operations like in a java program. The Code is as follows:

Private Button myButton = null; myButton = (Button) findViewById (R. id. myButton); myButton. setText ("my first button ");
In fact, this process only reflects the xml file and R. java file connection (through this sentence: android: id = "@ + id/myButton), and. java and R. java connection (through this sentence: findViewById (R. id. myTextView ))

Code embodiment of other file relationships:

When the MyActivity. java file is in contact with the Main. xml file, it is reflected by setContentView (R. layout. main) in the MyActivity. java file, because an Activity file corresponds to a layout file.

When the connection between the MyActivity. java file and the AndroidManifest. xml file is passed in the AndroidManifest. xml file

<Activity android: name = ". myActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity>
This also shows one of the key points of Activity creation:
Every Activity should be configured in the AndroidManifest. xml file.

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.