编写一个基本的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,可以看到如下內容:
<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會自動添加框架代碼,如下所示:
<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如下所示:
<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如下所示:
<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的處理。添加代碼,內容如下:
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,代碼如下:
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,在緊接著上述代碼后添加代碼如下:
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。
轉載于:https://www.cnblogs.com/yunsean/archive/2010/11/03/1868180.html
總結
以上是生活随笔為你收集整理的编写一个基本的Android程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】javax.xml.transfo
- 下一篇: 文件,文件夹基本操作