上一节介绍的Eclipse中安卓程序的一些基本知识,这一节将创建第一个Android程序。
新建Android项目,向导中的参数具体如下图:

我们将按两种方式来实现一个显示“Hello World ”的程序:
第一种是程序编码方式:
找到Text.java,在onCreate(Bundle savedInstanceState)方法里面,添加如下代码:
TextView textView = new TextView(this); textView.setText("Hello World "); setContentView(textView);
右击项目,run as Adroid Application,稍等一会,如果安卓环境和调试ADV没有问题,运行成功的结果如下图:

第二种是按照xml配置:
注释掉刚刚添加的代码,找到res目录,可以看到layout目录和values目录,打开strings.xml,上一节我们说到,这个文件中存放的是一些字符等常量,在resources标签内添加如下代码:
<string name="showInfo">我的第一个安卓程序</string>
找到layout目录下的main.xml文件,将
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
中的
android:text="@string/hello"
改成
android:text="@string/showInfo"
保存,运行程序,将出现如下的结果:

通常情况下,创建组件放在xml中比放在程序中比较容易修改和配置,可以以程序的方式为组件添加事件,两者相互结合使用。