Document directory
Preface
In the previous section, we used a demo to familiarize ourselves with the basic use of Eclipse, how to run it in simulators and mobile phones, and how to package it into an APK, but did not write the code in detail, I believe many of you can't afford it anymore. In this section, we will write code to familiarize ourselves with the situation of Android SDK and some java features.
Statement
This series of articles is not a tutorial, but only for notes. please correct me if you have any mistakes.
Welcome to reprint, reprint Please retain the original source: http://www.cnblogs.com/rayee
Body
First, let's take a look at the things involved in programming, get familiar with the basic components of the SDK, and then write a very simple small program.
I. Analysis demo
In the project demo created in the previous section, open demoactivity. Java under src/COM. Android. Demo, as shown below:
There are only 13 lines of DEMO code. Let's analyze them one by one:
Package COM. android. demo;/** declare that the code of this file belongs to the package "com. android. demo "*/import android. app. activity;/** reference the activity component from the android SDK */import android. OS. bundle;/** reference bundle component from Android SDK */public class demoactivity extends activity {/** create class demoactivity, inherit activity, it has the activity features * // ** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {/** rewrite the function of the same name of the base class. When the program runs, it is called */super. oncreate (savedinstancestate);/** call the same name function of the base class */setcontentview (R. layout. main);/** set the attempt to display the interface */}}
Line 3 declares the package of the Code. The package name must correspond to the package where the file is located. Otherwise, an error is reported.
Lines 3rd and 4 Reference Components. The "Reference" here does not include files like include in other languages. It only tells the compiler that it will be used later.
For more information about package and import, >>> click here.
Row 3 creates a new demoactivity class that inherits the activity. Java is a fully object-oriented language, and everything should be encapsulated in the class. Friends who are not familiar with the object-oriented language are probably not used to it (I used PHP for web development before, write implementation wherever possible ...). The demoactivity class inherits the features of the activity and belongs to an "activity" (as mentioned earlier, activity is a basic component in Android and is very basic, any program with an interface has it ).
If you want to learn more about "object-oriented",> here.
Line 2: Reload the oncreate function. If you have programming experience, you will know that the on prefix function is a callback function. It is a small affected and can only be called at the beginning of the activity interface.
Row 3 calls a function of the same name as the base class. There are still some things to deal with in the base class. You must notify him.
Row 3: Specifies the view used to display the interface. Literally, we can see that setcontentview (R. layout. Main) uses R. layout. Main as an attempt. What does R. layout. Main represent? Where can we start with the change?
In the previous section, we introduced a folder Gen automatically maintained by IDE, which contains an R. java file, some people may understand, here is the reference content, as shown below:
R. layout. Main is an attribute in the R. layout class, with a value of 0x7f030000 and a resource ID. Where does it point?
As you can see, the true view layout file is here. As mentioned in the previous section, resources in the res folder will automatically generate records in R. java. Now we can see that this is indeed the case. As for the resource ID generated based on what rules, we don't have to study it. It's just as fun to maintain it automatically.
You can start with modifying the program interface later.
The demo program has so much code, isn't it that difficult? Haha.
Students who have learned Java may have questions: why are there no entry functions?
We write Android programs in the android SDK. the SDK completes a lot of work behind the scenes, including where the program runs and how to render the view, we only need to write the logic part and view part of the program. For the code in this program, the demoactivity class is equivalent to an entry (let's call it an entry activity ), this is a little different from the native Java programming.
NLP colleagues who like to brainstorm think: A program may have multiple interfaces and multiple activities. How do you know which activity to choose as the portal?
The previous section also mentioned a global configuration file androidmainifest. xml. Let's take a look:
We can see that all activity information is configured in this file. demoactivity contains two intent-filters, <action Android: Name = "android. intent. action. main "/>, <category Android: Name =" android. intent. category. launcher "/>. The launcher is found. You can guess this is the entry mark. Let's verify it later.
2. Create an activity
Let's create a new activity, instead of automatically generated demoactivity.
Right-click com. Android. Demo under Src-> New-> class
Enter the class name in "name", "launcheractivity" here, and "android" in "superclass. app. activity is the base class of the activity. You do not need to modify the rest. Click "finish". The result is shown in:
As you can see, the launcheractivity is created and some code is automatically generated, which is really convenient.
Worse, the demoactivity class starts with an upper-case letter, and we start with a lower-case l. Let's unify it:
In launcheractivity. modify the class name in Java directly and change l to L. The IDE reports an error (Because Java is case sensitive and the class name must be the same as the file name ), move the mouse to the wrong place and the modification suggestion is displayed. We select "RENAME compilation unit... ", As shown in:
The error message disappears and the file name starts with an uppercase letter. This is really convenient...
Introduce the bundle component and import Android. OS. Bundle;
Reload the oncreate method. This must be reloaded. we have to set the view in it. The operation is as follows:
Right-click the launcheractivity file, select Source-> override/implement methods..., check oncreate, and click "OK". The code is automatically generated:
3. Create a layout
There is no view for this activity. We create a new one:
Right-click Res/layout and choose new> Other.
Select Android XML layout file, click "Next", enter the layout name "launcher" on the next page, and click "finish". The layout file is created:
The current SDK comes with a visual layout, which can be dragged up directly. However, to make a good-looking interface, you also need to have a deep understanding of the android interface layout. I will explain it in detail later.
You can click the tag to switch between the visual interface and the XML editing interface. This switch button is available for many XML pages.
Here we will create a startup interface, adding a loading icon in the middle and a line of text. The detailed process is not described much (because I have not fully learned how to deploy it, and do not dare to misunderstand it). The final interface is as follows:
The content of the XML file is as follows, which can be copied and pasted to the file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="120dip" android:text="Welcome ..." android:textAppearance="?android:attr/textAppearanceLarge" /> </FrameLayout></LinearLayout>
Add the code for setting the view in the oncreate method of the launcheractivity class:
setContentView(R.layout.launcher);
In this way, the Java code has been compiled, but it is not complete yet. We still need to write it in androidmainifest. add the configuration information of the activity to the XML file, and move the intent-filter in the demoactivity configuration to the launcheractivity configuration to verify whether it is an entry flag. The final configuration information is as follows:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".DemoActivity" android:label="@string/app_name" > </activity> <activity android:name=".LauncherActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
The compilation of a new activity is complete. Follow the method in the previous section to see if it is correct:
We can see that the activity we have compiled is running. As expected, the configuration information in the intent-filter segment is indeed used to mark the entry activity.
I have been running the program for a long time or loading it all the time. Why? Because there are no subsequent actions in the program. Next, modify the program to jump to demoactivity.
4. jump between activities
This requires modifying the code in the launcheractivity class. The idea is to wait for a while after oncreate is called and the view is set, and then jump to demoactivity. Start encoding:
Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubIntent goIntent = new Intent();goIntent.setClass(LauncherActivity.this, DemoActivity.class);startActivity(goIntent);}}, 3*1000);
Here we have created a timer, added a task, and executed it three seconds later. The code to be redirected is located in lines 6th, 7, and 8:
Create an intent, set the start location (launcheractivity), destination vertex (demoactivity), and start to jump (startactivity ).
This code is located after setcontentview (R. layout. launcher); the final code is as follows:
package com.android.demo;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class LauncherActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.launcher);Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubIntent goIntent = new Intent();goIntent.setClass(LauncherActivity.this, DemoActivity.class);startActivity(goIntent);}}, 3*1000);}}
Save and run again to see if the welcome page jumps to the Home Page 3 seconds later.
V. Legacy issues
Some new things have been introduced in this section. I did not explain them before. Some may have long been in doubt. Here I will focus on it:
BytesWhat is the bundle introduced by the second Import Statement, which has never been used in the future?
Bundle literally means bundle, bundle, and a group of things. It is used to transmit data in Android and is represented as a key-value pair. data can be transmitted from one activity to another. In this example, we haven't used it yet, but the oncreate function parameter must be, so we must import...
BytesFinally, an intent is used. What is it?
Intent, intent, similar to bundle, also contains a group of things, but not only data, but also information such as Operation types and operation objects. In the above example, the startactivity function only requires one gointent to implement the jump function, because the gointent contains the things required for the operation. If you need to transmit other information, you also need more complex settings.
End
In this section, we have compiled a complete small program, although it is practically useless...
Think about a lot of things you have come into contact with, such as object-oriented, inheritance, resource reference, global configuration, activity creation, layout file creation, and activity redirection.
Today, I still failed to write a cow program. Maybe the students think it is too slow, but as long as I keep learning a few knowledge points and new things every day, I will definitely become a cow!
In the next section, we will write a ticket to locate the base station and learn about the call of other APIs, HTTP Communication, JSON parsing, and other related knowledge in the SDK.
If you like to see these texts, click "recommendation" in the lower right corner to support me. Thank you!