编写一个基本的Android程序
程序员有个惯例,在任何一个新平台上学习一门新语言的时候,首先做的第一件事情就是编写一个Hello,World程序,在Android上也不例外,《Hello,World》的第一个程序也是Hello,World(好像绕口令?)。但由于Eclipse的辅助,要实现一个Hello,World已经没有任何难度(甚至不需要编写任何代码)。
但今天,我想写的第一个程序有点不同:我的程序需要把Eclipse默认的Hello,World先干掉,然后,在界面上添加一个按钮,点击按钮显示出Hello,World的弹出对话框。
开始了……
1、兵马未动,粮草先行。
- 启动Eclipse……
2、新建工程。
- 选择主菜单中的“File—New...—Other”,在弹出的对话框中选择“Android/Android Project”,点击Next;
- 在New Android Project对话框中,做如下设置:
- Project name:HelloWorld
- Contents:维持默认值
- Build Target:有什么选什么(什么都没有?回去搭建ADT开发环境吧)
- Application name:Hello,World
- Package name:org.example.helloworld
- Create Activity:HelloWorld
- 点击Finish完成工程创建。
3、试运行初始环境。
- 这个时候运行程序,自动启动仿真器,你会看到程序如下运行界面:
这就是传说中的Hello,World了,什么都不需要做吧!
接下来实现我们的目标了。
4、修改主Activity资源。
- 编辑资源:Hello,World/res/layout/main.xml,这个文件是主Activity所对应的界面布局(在AndroidManifest.xml中定义);
- 打开main.xml,可以看到如下内容:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
主界面上的Hello World,HelloWorld就是由上述代码中的android:text="@string/hello";@string/hello表示使用字符串定义中的“hello”这个定义,该定义可以通过打开Hello,World/res/values/string.xml查看,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello,World</string>
</resources>
@string/hello即对应<string name="hello">Hello World, HelloWorld!</string>中的内容。
-
- 添加按钮。为主Activity添加一个名为About的按钮(我们的目标是通过单击该按钮显示出一个新的页面)。
在main.xml中添加一个按钮(Button)对象,修改后的main.xml内容如下:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label"
/>
</LinearLayout>
-
- 完成添加后,会发现Eclipse在<Button这一行标注了一个红叉,这是因为我们还么有定义所需的资源,不要紧,先不管他。
- android:id="@+id/about_button",这一行表示为该按钮添加(+号)了ID,id名称为about_button,这个名称在后续的代码中会用到。
- android:text="@string/about_label",这一行指定该按钮显示的文字内容,@string/about_label表示内容由string.xml中的about_label节点定义,但目前我们尚未修改string.xml,所以eclipse会报告错误(显示一个红叉)。
- 完成添加后,会发现Eclipse在<Button这一行标注了一个红叉,这是因为我们还么有定义所需的资源,不要紧,先不管他。
接下来我们解决这个红叉的问题。
- 添加字符串定义。修改string.xml,加入about_label的定义,修改后的string.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello,World</string>
<string name="about_label">About</string>
</resources>
<string name="about_label">About</string>即为新添加的about_label定义。
-
试运行。
修改string.xml后,main.xml中的红叉已经消失,这时候可以再次运行代码,执行结果如下:
对比前图,可以发现我们新添加的“About”按钮。
5、添加一个新的Activity。
HelloWorld这个Activity(HelloWorld.java)是eclipse在建立工程时帮我们建立好的,我们需要再新建一个About的Activity用于在单击About按钮时显示。
- 新建About Activity对应的布局文件。
- 在Hello,World/res/layout上单击右键,选择“New—Other...”;
- 在弹出的“New”对话框中选择”Android/Android XML File",单击“Next”;
- 在“new Android XMLFile”中,只需要修改“File”字段,键入:about.xml;
- 单击Finish,完成文件的创建。
- 修改About布局。
- 新创建的about.xml,ADT会自动添加框架代码,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
-
- 为Layout添加一个View,以显示“About ‘Hello World’”信息(我们的终极目标嘛),修改后的about.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_info"
/>
</LinearLayout>
-
- 修改main.xml类似,我们使用了@string/about_info来指定新添加的TextView显示的内容。因此我们同样需要修改string.xml来定义该标识,修改后的string.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello,World</string>
<string name="about_label">About</string>
<string name="about_info">About "Hello,World"</string>
</resources>
- 添加Activity对应的类。
至此,我们只是完成了新Activity的布局创建,我们还需要创建该Activity的代码(现在该代码的主要功能就是加载Layout进行显示)。
-
- 在Hello,World/src/org.example.helloworld上单击右键,选择“New—Class...";
- 在弹出的“New Java Class”对话框中,修改如下信息(未提及的保持默认值):
- Name:About(新的Activity取名为About);
- Superclass:android.app.Activity(从android.app.Activity派生该类型,看看HelloWorld.java,他也是这么干的);
- 修改后的配置如下:
-
- 单击Finish完成代码的创建。
查看Package Explorer,在org.example.helloworld下多了一个名为About.java的文件,该文件包含唯一一个名为About的类,如下图所示:
-
- 双机About.java,内容如下:
package org.example.helloworld;
import android.app.Activity;
public class About extends Activity {
}
这次,eclipse什么都没有帮我们做。我们需要处理该Activity创建是的消息:加载布局文件进行显示。
-
- 添加对onCreate的处理。添加代码,内容如下:
package org.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
-
-
- import android.os.Bundle;因为onCreate的参数需要使用Bundle对象;
- super.onCreate(savedInstanceState);必须调用基类的重载函数,否则等着死翘翘;
- setContentView(R.layout.about);加载显示About这个Layout。
-
6、在主Activity中激活About对话框。
我们的终极目标是通过点击主页面上的“About”按钮显示出About页面。因此我们需要在HelloWorld.java中的HelloWorld类中处理About按钮的点击事件并显示出About页面。
- 修改HelloWorld类,修改后的代码如下:
package org.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class HelloWorld extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View v = findViewById(R.id.about_button);
v.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId())
{
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
-
- 添加类型所涉及的引用:
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
这些类型分别被后边的代码所使用。
-
- 为About按钮添加侦听:
View v = findViewById(R.id.about_button);
v.setOnClickListener(this);
-
- 这里需要让HelloWorld实现OnClickListener接口,以及接口的onClick方法:
public void onClick(View v){
switch(v.getId())
{
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
-
- 至此,代码修改完成。
- 测试代码,试运行。
- 在主界面上点击About按钮,这些About有响应了,但……
Oh,My God。程序崩溃了。
7、注册Activity。
程序没有按照我们预料的结果显示出About对话框,反而直接崩溃了。原因呢……
别看代码了,代码肯定没有错,错误在于……
- 在中AndroidManifest.xml申明Activity。
在Android中,需要被用到的每一个Activity必须在AndroidManifest.xml中被申明(We forgot one important step: every activity needs to be declared in
AndroidManifest.xml.)
- 打开,可以看到ADT在生成时已经帮我们申明了HelloWorld这个Activity,代码如下:
<activity android:name=".HelloWorld"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- 同样,我们需要申明About,在紧接着上述代码后添加代码如下:
<activity android:name=".About"
android:label="@string/about_title">
</activity>
为什么不要action和category两个节点呢?他们是主Activity所需要的,About是主吗?no,当然不是。
android:label="@string/about_titile",这一句是指定该Activity的标题,使用string.xml中的about_title字段。
同样的,eclipse会出现一个小红叉,提示我们没有定义about_title,那就修改string.xml吧,修改后内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">Hello,World</string>
<string name="about_label">About</string>
<string name="about_info">About "Hello,World"</string>
<string name="about_title">About</string>
</resources>
8、终于要发了,见8就发,哈哈。
至此为止,所有的工作都已经结束了,再点击Run开始运行吧(调试都免了),主界面肯定没问题,直接点About按钮吧,运行效果如下:
一切Over。
小插曲:
如果你的工程在一开始或者中间某个时候出现了错误:Conversion to Dalvik format failed with error 1。(我就遇到了)
解决方案:选择主菜单:Project —Clean—Clean project selected below,Ok。