It took more than a year to get things done on and off Android. At the beginning, I made a software zinfo together with my buddy zeaster to participate in the Google Android competition, this is an SNS concept and information sharing platform. For more information, visit our website infosword.
But last year we stopped development -. -) if any brother is interested in him or has more and better ideas, please email me [haric.zhu@gmail.com] and may be able to reorganize and continue development in the future.
I have always wanted to write a blog to share my learning experience and development experience, but I have never insisted on it because of laziness. Because I have been learning experience from various prawns on the internet, and I feel uneasy when I dive, I plan to share my experience and Thoughts on Android with you here, I also urge myself to study hard and learn from each other every day.
[Body]
Let's talk about it in a few minutes. Let's take an enhanced version of helloworld to introduce the general situation and program structure of Android. This helloworld program is very simple and has the following interface:
Click the button to bring up a dialog box containing Hello world! -.-
The following is a step-by-step development process. In this process, I will explain in detail the android project structure.
1. Install the development environment:
Google recommends that we use (eclipse with the ADT plugin). ADT is the development plug-in of Android. It provides some debugging tools and so on. I have a detailed introduction on the android site of Google Code, according to his standards, I will not talk too much [
Http://code.google.com/android/intro/installing.html
]
2. Create a project:
Open eclipse, create a project, and select other, and then select Android project. In this case, a dialog box named title new Android project is displayed, as shown in:
3. Introduce the project structure:
The following figure shows the project structure:
Src com. haric. there is a helloworld under hello. java, whose name comes from the acivity name we entered when creating a project. helloworld inherits from activity (the most important class in Android framework, for more information, see-> (Activity
), We can easily understand that it is a UI container and directly deals with the front-end class with users.
There is another R. java, this class is automatically generated by the system based on the content in the res folder. let's first talk about the res folder. Res is short for resources. As the name suggests, the text and images you need in your program, layout files and other resources are placed under this folder. Now you can see that there are
Drawable-This is for storing images.
Layout-This is for layout files.
Values-Put the following string (Strings. xml), Color (Colors. xml), Array (Arrays. xml)
(There are more things in res. For more information about resouces compatibility, see (Resources
))
Android helps us manage all these resources, and the role of content reclamation is obvious, making it easier to internationalize, when using the same resource, it is also convenient and saves space (Global Reference), the content in the res folder changes, R. java will re-compile and synchronize updates, so you do not need to manually update this class.
Finally, androidmanifest. xml. Every time you add an acivity, you must describe it in this file. This file is very important and has many things. We will detail this file in the future. (To Me: Leave a link in this place and then link it -.-)
4. design the interface layout:
Next we will use R. java. First, we will create a file Hello _ world. XML in layout. The editing content is as follows:
XML Code
- <? 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"
- >
- <Button Android: Id = "@ + ID/button01"
- Android: text = "@ string/click_me"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- </Button>
- </Linearlayout>
<? 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"> <button Android: id = "@ + ID/button01" Android: text = "@ string/click_me" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> </linearlayout>
Some people asked the main. xml file. I suggest you change its name to hello_world.xml or delete it and add a new hello_world.xml file,[Tips]
Because there are many activities in the project, it is best to hook your layout file name with the corresponding activity for future editing and use.
After this step is completed, what is the result? It must not be compiled. Hey hey, serious students must have noticed that
"Android: text =" @ string/click_me ". This line specifies the text displayed on your button. We need to add it to RES/values/strings. add a <string name = "click_me"> click me </string> in XML to reference this string resource in the layout file. After the layout is completed, this is used in the program.
5. Use the layout in the program:
Open helloworld. Java and edit the content as follows:
Java code
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. hello_world );
- }
Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. hello_world );}
R. layout. hello_world points to hello_world.xml. Similarly, R. String. click_me points to "click
Me ", run it (right-click your project, run as-> Android Application) to see a button
6. Add a click event for the button:
To add a click event for the button, we need to first get this object, then set the listener, and then write The onclick event
The Code is as follows:
Java code
- Import Android. App. activity;
- Import Android. App. alertdialog;
- Import Android. OS. Bundle;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Public class helloworld extends activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Button button = (button) findviewbyid (R. Id. button01 );
- Button. setonclicklistener (New onclicklistener (){
- Public void onclick (view arg0 ){
- Opendialog ();
- }
- });
- }
- Private void opendialog (){
- Alertdialog. Builder = new alertdialog. Builder (this );
- Builder. settitle ("hello ");
- Builder. setmessage ("Hello world \ n ");
- Builder. setnegativebutton ("OK", null );
- Builder. Show ();
- }
- }
Import android. app. activity; import android. app. alertdialog; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class helloworld extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. button01); button. setonclicklistener (New onclicklistener () {public void onclick (view arg0) {opendialog () ;}});} private void opendialog () {alertdialog. builder = new alertdialog. builder (this); builder. settitle ("hello"); builder. setmessage ("Hello world \ n"); builder. setnegativebutton ("OK", null); builder. show ();}}
Button button = (button) findviewbyid (R. id. button01); this statement is used to obtain the interface control object set in layout. This ID is Android: Id = "@ + ID/button01" specified in the button ".
Android UI is similar to SWT. I will introduce some frequently-used and interesting UI controls in detail in the future. Write it here today, and I will paste the source code of the corresponding project to the bottom.
- Hello-world-plus.rar (24.6 KB)
- Description: source code.
- Downloads: 785