Learning notes for the first line of code -- Chapter 1 starts, your first line of Android code, the first line of code notes
1.3 create your first Android project.
1.3.1 create a HelloWorld Project
1. Application Name indicates the Application Name, which is displayed on the mobile phone;
2. Project Name indicates the Project Name, which is displayed in Eclipse;
3. Minimum Required SDK refers to the Minimum compatible version of the program, and Compile With refers to the compiled version of the program;
1.3.3 analyze your first Android Program
1. src is the place where all of our Java code is placed;
2. The content in the gen directory is automatically generated. There is mainly an R. java file. Any resource you add in the project will generate a corresponding resource id here. This file should never be modified manually;
3. The assets Directory is not used much. It mainly stores some files packaged with the program and can dynamically read the content of these files when your program is running. In addition, if your program uses the WebView function to load local webpages, all webpage-related files are stored in this directory ;'
4. The bin directory contains files automatically generated during compilation. The installation package is in this file;
5. libs directory. If you use third-party Jar packages in your project, you need to put these Jar packages under the libs directory, jar packages placed in this directory will be automatically added to the build path;
6. res directory. To put it simply, all the images, la S, strings, and other resources you use in the project are stored in this directory. the content in java is automatically generated according to the files in this directory;
7. AndroidManifest and xml are the configuration files of your entire Android project. All the four components you define in the program must be registered in this file. In addition, you can add a permission statement to the application in this file, or re-define the minimum compatible version and target version of the program you specified when creating the project. '
8. The project. properties file specifies the SDK version used to compile the program using a line of code. The HelloWorld project uses API 14. You can change it to another version for a try;
9. Next we will analyze how the HelloWorld project runs, open the AndroidManifest and xml files, and find out the code:
<Activity
Android: name = "com. example. helloworld. HelloWorldActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/> // The two lines of code indicate that HelloWorldActivity is the main activity of the project.
</Intent-filter>
</Activity>
Note: This code indicates registration of the HelloWorld activity. Activities not registered in AndroidManifest. xml cannot be used.
10. When introducing the four Android components, we said that activity is the facade of the Android application. Everything you see in the application is put in activity. So when we open the program, what you see is actually the HelloWorldActivity. The code for this activity is as follows:
Package com. example. helloworld;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Public class HelloWorldActivity extends Activity {// indicates that HelloWorldActivity inherits from Activity. Activity is an Activity base class provided by the Android system. All activities in our project must inherit the Activity features.
@ Override
Protected void onCreate (Bundle savedInstanceState) {// onCreate () method is a method that must be executed when an activity is created.
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_hello_world); // The setContentView () method called here is that this method introduces a layout for the current activity.
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu) {// create a Menu.
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. hello_world, menu );
Return true;
}
}
After reading this code, I found that there is no HelloWorld in it. It is also the HelloWorld displayed on the program interface during the activity! Where is it defined?
In fact, the Android program design focuses on the separation of logic and views. Therefore, it is not recommended to write the interface directly in the activity. A more common approach is to compile the interface in the layout file, and then introduce it in the activity. That is to say, HelloWorld is defined in the hello_world_layout.xml file? The code for Opening this file is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". HelloWorldActivity">
// It doesn't matter if you don't understand the above. Let's take a look at the following code.
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</RelativeLayout>
TextView is a control provided by the Android system to display text in the layout. We found the HelloWorld in this Code, but is this defined here? Not here. Real HelloWorld! The string is not defined here. Android does not recommend hard encoding strings in programs. A better way is to define strings in res/values/string. xml and then reference them in layout files or code. The following is the strings. xml code:
<Resources>
<String name = "app_name"> Hello World </string> // modify the APP name
<String name = "action_settings"> Settings </string>
<String name = "hello_world"> Hello world! </String> // uses a key-value pair, HelloWorld! The value corresponds to a key called hello_world. In the hello_world_layout.xml layout, the corresponding value is found by referencing the hello_world key.
</Resources>
1.3.4 detailed description of resources in the project
1. res Directory: All Folders starting with drawable are used to put images, all folders starting with values are used to put strings, and layout folders are used to put layout files, the menu folder is used to store menu files.
2. How to use these resources?
For example, in strings. xml just mentioned, find Hello world! String, which can be referenced in two ways:
(1) You can use R. string. hello_world in the code to obtain a reference to this string;
(2) In XML, @ string/hello_world can be used to obtain reference to this string;
The basic syntax is the above two points. The string part can be replaced. If it is a referenced image resource, it can be replaced with drawable. If it is a reference layout, it can be replaced with layout, and so on.
In addition, the HelloWorld project icon is in AndroidManifest. in xml, android: icon = "@ drawable/ic_launcher" is specified, and the image ic_launcher is in the drawable folder. If you want to modify the project icon, change it here.
1.4 understand the use of log tools
1.4.2 use Android log Tool
1. the verbose level corresponding to Log. v () is used to print the most trivial and meaningful Log information. It is the lowest level in Android logs.
2. Log. d () is used to print some debugging information, which is helpful for debugging programs and analyzing problems. Corresponding levels of debug.
3. The Log. I () method is used to print some important data. This data should be what you really want to see and can help you analyze user behavior. Corresponding Level info.
4. The Log. w () method is used to print some warning information, prompting that the program may have potential risks in this place. It is best to fix the warning location, corresponding to the level of warn.
5. Log. e () is used to print error messages in the program. Corresponding Level error.
1.4.3 how to use the log Tool
Public class HelloWorldActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_hello_world );
Log. d ("HelloWorldActivity", "onCreate execute"); // Log. d. Two parameters are passed in. The first miserable number is tag. Generally, the current class name is passed in, which is mainly used to filter printed information. The second parameter is msg, that is, the specific content to be printed.
}