开发你的第一个android程序 Hello world

来源:互联网 发布:数据切片 数据切块 编辑:程序博客网 时间:2024/05/01 18:38

http://developer.android.com/intl/zh-CN/resources/tutorials/hello-world.html

As a developer, you know that the first impression of adevelopment framework is how easy it is to write "Hello, World." Well,on Android, it's pretty easy. It's particularly easy if you're usingEclipse as your IDE, because we've provided a great plugin that handlesyour project creation and management to greatly speed-up yourdevelopment cycles.


If you're not using Eclipse, that's okay. Familiarize yourself with Developing in Other IDEs. You can then return to this tutorial and ignore anything about Eclipse.
Before you start, you should already have the very latest SDKinstalled, and if you're using Eclipse, you should have installed theADT plugin as well. If you have not installed these, see Installing the Android SDK and return here when you've completed the installation.Create an AVD
To learn more about how to use AVDs and the options available to you, refer to the Android Virtual Devices document.
In this tutorial, you will run your application in the AndroidEmulator. Before you can launch the emulator, you must create anAndroid Virtual Device (AVD). An AVD defines the system image anddevice settings used by the emulator.
To create an AVD, use the "android" tool provided in the Android SDK. Open a command prompt or terminal, navigate to the tools/ directory in the SDK package and execute: android create avd --target 2 --name my_avd
The tool now asks if you would like to create a custom hardwareprofile. For the time being, press Return to skip it ("no" is thedefault response). That's it. This configures an AVD named "my_avd"that uses the Android 1.5 platform. The AVD is now ready for use in theemulator.
In the above command, the --target option is required and specifies the deployment target to run on the emulator. The --name option is also required and defines the name for the new AVD.Create a New Android Project
After you've created an AVD, the next step is to start a new Android project in Eclipse.

  • From Eclipse, select File > New > Project.     
    Ifthe ADT Plugin for Eclipse has been successfully installed, theresulting dialog should have a folder labeled "Android" which shouldcontain "Android Project". (After you create one or more Androidprojects, an entry for "Android XML File" will also be available.)
  • Select "Android Project" and click Next.     
  • Fill in the project details with the following values:
    • Project name: HelloAndroid
    • Application name: Hello, Android
    • Package name: com.example.helloandroid (or your own private namespace)
    • Create Activity: HelloAndroid
    • Min SDK Version: 2
       
    Click Finish.   
       
    Here is a description of each field:    Project NameThis is the Eclipse Project name — the name of the directory that will contain the project files. Application NameThis is the human-readable title for your application — the name that will appear on the Android device. Package NameThisis the package namespace (following the same rules as for packages inthe Java programming language) that you want all your source code toreside under. This also sets the package name under which the stubActivity will be generated.
    Your package name must be unique across all packagesinstalled on the Android system; for this reason, it's very importantto use a standard domain-style package for your applications. Theexample above uses the "com.example" namespace, which is a namespacereserved for example documentation — when you develop your ownapplications, you should use a namespace that's appropriate to yourorganization or entity.              Create ActivityThis is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activityclass. An Activity is simply a class that can run and do work. It cancreate a UI if it chooses, but it doesn't need to. As the checkboxsuggests, this is optional, but an Activity is almost always used asthe basis for an application. Min SDK VersionThisvalue specifies the minimum API Level required by your application. Ifthe API Level entered here matches the API Level provided by one of theavailable targets, then that Build Target will be automaticallyselected (in this case, entering "2" as the API Level will select theAndroid 1.1 target). With each new version of the Android system imageand Android SDK, there have likely been additions or changes made tothe APIs. When this occurs, a new API Level is assigned to the systemimage to regulate which applications are allowed to be run. If anapplication requires an API Level that is higher than the level supported by the device, then the application will not be installed.     
    Other fields:The checkbox for "Use default location" allows you to change thelocation on disk where the project's files will be generated andstored. "Build Target" is the platform target that your applicationwill be compiled against (this should be selected automatically, basedon your Min SDK Version).   
    Notice that the "Build Target"you've selected uses the Android 1.1 platform. This means that yourapplication will be compiled against the Android 1.1 platform library.If you recall, the AVD created above runs on the Android 1.5 platform.These don't have to match; Android applications are forward-compatible,so an application built against the 1.1 platform library will runnormally on the 1.5 platform. The reverse is not true.  


Your Android project is now ready. It should be visible in the Package Explorer on the left. Open the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid). It should look like this:package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Notice that the class is based on the Activityclass. An Activity is a single application entity that is used toperform actions. An application may have many separate activities, butthe user interacts with them one at a time. The onCreate()method will be called by the Android system when your Activity starts —it is where you should perform all initialization and UI setup. Anactivity is not required to have a user interface, but usually will.
Now let's modify some code! Construct the UI
Take a look at the revised code below and then make the same changesto your HelloAndroid class. The bold items are lines that have beenadded.package com.android.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);

   }
}
Tip: An easy way to add import packages to your project is to press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse shortcut that identifies missing packages based on your code and adds them for you.
An Android user interface is composed of hierarchies of objects called Views. A Viewis a drawable object used as an element in your UI layout, such as abutton, image, or (in this case) a text label. Each of these objects isa subclass of the View class and the subclass that handles text is TextView.
In this change, you create a TextView with the class constructor, which accepts an Android Contextinstance as its parameter. A Context is a handle to the system; itprovides services like resolving resources, obtaining access todatabases and preferences, and so on. The Activity class inherits fromContext, and because your HelloAndroid class is a subclass of Activity,it is also a Context. So, you can pass this as your Context reference to the TextView.
Next, you define the text content with setText(CharSequence) setText().
Finally, you pass the TextView to setContentView()in order to display it as the content for the Activity UI. If yourActivity doesn't call this method, then no UI is present and the systemwill display a blank screen.
There it is — "Hello, World" in Android! The next step, of course, is to see it running.Run the Application
The Eclipse plugin makes it very easy to run your applications:

  • Select Run > Run.
  • Select "Android Application".


To learn more about creating and editing run configurations in Eclipse, refer to Developing In Eclipse, with ADT.
The Eclipse ADT will automatically create a new run configurationfor your project and the Android Emulator will automatically launch.Once the emulator is booted up, your application will appear after amoment. You should now see something like this:

The "Hello, Android" you see in the grey bar is actually theapplication title. The Eclipse plugin creates this automatically (thestring is defined in the res/values/strings.xml file and referenced by your AndroidManifest.xml file). The text below the title is the actual text that you have created in the TextView object.
That concludes the basic "Hello World" tutorial, but you shouldcontinue reading for some more valuable information about developingAndroid applications.

下面是中文讲解
在学习完第一课和第二课之后,相信你对android已经有了初步的了解,并且已经搭建好了开发平台.  


1.创建AVD(Android Virtual Device安致模拟设备),就是建立一个模拟的android手机,其实我们在上一课已经创建好了,再来查看一下.eclipse的window菜单,打开android AVD and SDKManager,在这里就可以管理AVD和SDK,如果没有AVD的话,就新建一个吧,平台和分辨率之类的可以根据自己的意愿选择,这里我们选择android 2.1的平台.(对于我们第一个简单的hello world程序而言,选哪个平台其实也没区别.)


2.选择 Eclipse,  File > New > Project. 在打开的窗口里选择Android>Android Project 点下一步,输入程序名(你创建的程序会在这个程序名的文件夹内),接下来可以参考上面的图片输入,
       下面是一些说明


    Project Name -------  在计算机中存储工程的目录的名字     
    Package Name -------  包名, 参考Java相关的概念     
    Activity Name -------  UI界面窗口的类名,从Activity继承而来     
    Application Name -------  应用程序的名称


Min SDK Version 最低SDK版本,如果写2的话,就代表包括1.1 和1.1以上版本的SDK都能运行,写3的话1.1的平台就不能运行了,最终体现在xml申明文件里

3.    左边列表里点开 HelloAndroid > src > com.example.helloandroid package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}




4.      可以运行了 run android application

5.      模拟器开机比较慢,等一回开了会自动运行,在程序列表里也能看到Hello,radroid 程序了

原创粉丝点击