Android Hello World实例介绍
一、安装开发平台和工具
1、Android SDK安装;
2、Eclipse(ADT Plugins)安装;
二、AVD创建
打开Android SDK And AVD Manger,创建一个AVD(Android Virtual Device)-虚拟设备(后续开发、调试用),假定版本为2.1-update1;
三、创建一个Android Project
1、在Eclipse中选择创建一个Android项目;
2、选择与之前创建的AVD一样的目标框架(Bulid Target):2.1-update1
3、各项属性填写如下:
- Project name: HelloAndroid
- Build Target: Select a platform version that is equal to or lower than the target you chose for your AVD.
- Application name: Hello, Android
- Package name: com.example.helloandroid (可以自定义自己的命名空间)
- Create Activity: HelloAndroid
package com.example.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); } }说明:

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello"/>
利用这种方式,开发者可以快速的定义应用的UI框架,同时,该方式很好的实现了UI代码和业务逻辑代码的分离!
下面说明下上述XML元素的几个属性含义:
Attribute | Meaning |
---|---|
xmlns:android | XML命名空间,必须包含。 |
android:id | 元素ID。该ID可以来自代码中的View类ID或者其他XML Layout。 |
android:layout_width | 定义当前View的宽度(本例为全屏宽度)。 |
android:layout_height | 定义当前View的高度。 |
android:text | 设置Textview元素要显示的文本内容。本例中,定义了一个“串资源”来定义text。"hello"是在res/values/strings.xml中定义的一个元素。这是Android推荐的串资源定义方式,因为这种方式在实现多语言等应用时会十分方便,将资源文件和程序代码分离。 |
/res/layout/main.xml
文件,将上面的xml内容拷贝并保存。另外,我们打开res/values/strings.xml,
修改其内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello, Android! I am coming!</string> <string name="app_name">Hello, Android</string> </resources>
最后,我们修改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); } }
此时,我们去除了TextView实现,而是用setContentView(R.layout.main)来实现UI绘制。其中类R由ADT自动生成,用来索引和定义各类资源元素,不要手动修改。
package com.example.helloandroid; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int textview=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }下面,我们重新保存应用并运行,效果如下:
至此,我们完成了Android上的第一个Hello
World,Enjoy IT!
- Project name: HelloAndroid
- Build Target: Select a platform version that is equal to or lower than the target you chose for your AVD.
- Application name: Hello, Android
- Package name: com.example.helloandroid (or your own private namespace)
- Create Activity: HelloAndroid