This article explains how to create and run the first Android program in eclipse.
Steps for setting up the environment in this article:
1. Download eclipse Version: Juno Release Eclipse IDE for Java Developers,
2. Install JDK 1.6.0 _ 33, download it from the official website, install it directly, and configure environment variables. If you do not understand, You can Google the "Java Development Environment"
3. download the latest Android SDK from the official website, start SDK Manager, select all Android versions and APIs, and wait for the download of tools.
4. start eclipse, Help-Install New Software. In the pop-up window, click Add and name ADT. After the URL is OK, Pending will appear. Wait. When Pending is changed to two options, select two options, press Next, click Accept, and then click Finish,
It starts to download. During the download process, you will be prompted to select OK. After the download is complete, you will be prompted to restart. After the restart, you will find more buttons,
Click Windows-preferance. In the displayed window, click Android,
By now, even if the environment has been built up, it may be omitted. If you find that the environment is unavailable, you can access the Internet to see other people's websites.
5. Create a virtual machine
Click this button on the right. In the pop-up window, click New on the right. In the pop-up window, we recommend that you set "Name" to "Android version", that is, "1.6" to "1.6,
Target selects the corresponding version, and the Size of the SD Card is enough to be written to MB. Skin: Select HVGA, so that the window will be smaller, choosing the default WVGA800 is a huge window and it is not easy to use.
Now the required environment has been successfully set.
Body:
Here, I will introduce how to create your first program. (It's not long before I learned Android. If I say something wrong, I hope I can leave a message to point out it .)
File-New-Project: Select Android Application Project
Application Name: display time and date
Project Name: showtimedata
Package Name: com. mmc. showtimedata)
Build SDK: Android 1.6 (1.6 is selected here because later versions are compatible with 1.6. Compile and run the SDK on the latest version after compiling the SDK under 1.6 .)
Minimum Required sdks: Android 1.6
For the remaining default values, go to the New Blank Activity page and press Main in the Activity Name. The remaining software will be changed automatically and then click Finish.
Create a project. Sometimes the software cannot create R. java file, so after the project is created, there will be a cross on the project icon. It doesn't matter. The fastest way is to delete the project and try again.
The first step below is to modify the activity_main.xml file, which is in res-layout and open. The following tag selects the display code.
[Java]
<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/btnShowData"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/showdata"
/>
<Button
Android: id = "@ + id/btnShowTime"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/showtime"
/>
</LinearLayout>
Here we can see android: text = "@ string/showtime" this type of code, here there is a @ string, is to call the key value in the values-strings.xml file in the project.
After activity_main.xml is modified, modify the strings. xml file,
[Java]
<Resources>
<String name = "app_name"> display the current time and date </string>
<String name = "hello_world"> Hello world! </String>
<String name = "menu_settings"> Settings </string>
<String name = "title_activity_main"> Main </string>
<String name = "showtime"> display current time </string>
<String name = "showdata"> display current date </string>
</Resources>
In this case, activity_main.xml can call @ string/showtime. Okay. Save
After editing, the following code starts to run.
According to the operation, it runs automatically. At the beginning, we created the 1.6 Virtual Machine. Note that we chose the Build version when creating the project, your virtual machine must have this version of virtual machine, otherwise it will not run.
If you set the virtual machine according to my settings, it should be displayed
As prompted, click the MENU button on the right and you will find that the two buttons you added are unavailable. There is only one external frame, so we will add materials to it below.
Next we need to modify the Main. java file under src.
[Java]
Package net. blogjava. showtimedata;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. OS. Bundle;
Import android. text. format. Time;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class Main extends Activity implements OnClickListener {
Private void showDialog (String title, String msg ){
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Builder. setIcon (android. R. drawable. ic_dialog_info );
Builder. setTitle (title );
Builder. setMessage (msg );
Builder. setPositiveButton ("OK", null );
Builder. create (). show ();
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btnShowData :{
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd ");
ShowDialog ("current Date", dateFormat. format (new Date ()));
Break;
}
Case R. id. btnShowTime :{
SimpleDateFormat dateFormat = new SimpleDateFormat ("HH: mm: ss ");
ShowDialog ("current time", dateFormat. format (new Date ()));
Break;
}
}
}
@ Override
Public void onCreate (Bundle savedInstanceStace ){
Super. onCreate (savedInstanceStace );
SetContentView (R. layout. activity_main );
Button btnShowDate = (Button) findViewById (R. id. btnShowData );
Button btnShowTime = (Button) findViewById (R. id. btnShowTime );
BtnShowDate. setOnClickListener (this );
BtnShowTime. setOnClickListener (this );
}
}
In the editing area, right-click source-override/Implement methods.
If you see a lot of forks During the editing process, that is, if there is a cross sign on the light bulb, you can click this sign and you will be prompted about the error, there are options for you to choose how to modify them and help you modify them.
If no package is imported, press CTRL + SHIFT + O to quickly add or delete unused packages. If you are not sure which package your function uses, you can check it on the official website or use the downloaded API,
The default path is C: \ Program Files \ Android \ android-sdk \ docs. You can open webpage Files.
After no error occurs, compile and run the same method.
At this time, you will find that the date and time can be displayed.