編輯:開發入門
現在來改寫現有的應用,讓它變成下圖那樣。這個例子的目的,是讓你熟悉幾個基本元件在真實應用中的用法:TextView、EditText、Button 與 Toast。
我們的應用會有一個標題欄,顯示「WELCOME!!!」。畫面主體是三個由上而下垂直排列的 TextView:第一個用預設字型顯示「Hello World, how're you?」;第二個是「Welcome to my first android app」,斜體襯線字型、置中對齊、白色背景;第三個是「Type anything and press the button」,粗體等寬字型、紅色背景。這三行之後是一個 EditText 供使用者輸入,再來是一個顯示「Click Me!」的按鈕。按下按鈕時,EditText 裡的任何內容都會以 Toast 的形式出現在螢幕下方中央。
這裡的實作程式碼並不多。真正該注意的是兩件事:如何用 ADT 的 GUI 編輯器把元件放進 XML,以及如何用 Eclipse 提供的「屬性」視圖快速設定元件屬性。這個例子也示範了 UI 與實作可以完全分開來寫。
圖 8. 帶元件的 Android 虛擬裝置

先在 strings.xml 裡補上幾個新字串,改掉既有的 welcome 字串,最後加上標題字串。另外為兩個 TextView 分別定義白色與紅色背景用的色彩資源。
清單 2. strings.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, how\'re you?</string>
<string name="app_name">Hello Android</string>
<string name="title">WELCOME!!!</string>
<string name="mainText">Welcome to my first android app</string>
<string name="subText">Type anything and press the button</string>
<color name="white">#FFFFFF</color>
<color name="red">#FF0000</color>
</resources>
我們把「hello」改成「Hello World, how're you?」——注意這裡的單引號必須用反斜線 \ 轉義。另外定義了兩個新字串 mainText 與 subText,分別搭配白色與紅色背景。
接著用 main.xml 的版面分頁拖進兩個新的 TextView、一個 EditText 與一個 Button,然後在屬性分頁逐一調整它們的屬性。
清單 3. main.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/mainTextView"
android:gravity="center"
android:text="@string/mainText"
android:textStyle="italic"
android:typeface="serif"
android:background="@color/white">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="20sp"
android:gravity="left"
android:id="@+id/subTextView"
android:padding="10sp"
android:text="@string/subText"
android:textStyle="bold"
android:typeface="monospace"
android:background="@color/red">
</TextView>
<EditText
android:text="Enter text here"
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<Button
android:text="Click me!"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
至此 UI 定義完成,剩下就是讓活動程式碼接上它。
這個檔案負責把 UI 與邏輯串起來。和 HelloWorld 一樣,我們用 Activity.onCreate(Bundle) 把畫面上的元件逐一備妥;而覆寫的 onClick(View) 則承載按鈕行為——讀取使用者輸入的文字,再用 Toast 顯示在螢幕上。
清單 4. MainActivity.java 檔案
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String title = getString(R.string.title);
setTitle(title); // done so that we can change the default title
// get handles to editText and button widgets
final EditText eText = (EditText) findViewById(R.id.EditText01);
final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// get the String entered into the editText widget
CharSequence editText = eText.getText();
// display the toast
Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();
}
});
}
}
用這段程式碼跑起來,就會得到圖 8 的結果。
值得留意的是,setTitle() 讓我們得以覆寫清單檔裡設定的預設標題;而 findViewById() 之所以找得到元件,靠的就是 main.xml 中 android:id="@+id/..." 產生的那些 ID。這正好印證了上一節提到的規則:要在 Java 裡操控元件,XML 裡就得先給它一個 ID。
Android 上的 XML 與 JSON 實戰(下):SAX 串流解析與 JSON 物件解析的取捨
先說清楚這個範例的定位 有個前提必須先講明白:這支範例程式並不會真的定時從網路抓資料回來。所有的 feed 都放在專案的 res/raw 資源目錄裡,所以程式關注的重點是
Android 開發入門(一):從 Android 歷史到應用架構
Android 的出現背景 在智慧型手機發展初期,BlackBerry 與 iPhone 各自佔據不同市場。前者偏向企業郵件與生產力工具,後者以消費娛樂與直覺操作著稱。A
智慧交通之路(二):開放資料社群與實用的通勤 App
City-Go-Round:開放交通資料的集散地 City-Go-Round 這個網站於 2009 年 12 月上線。它一方面在倡議開放交通資料——按它自己的統計,美國有
動手打造 Android RSS 閱讀器(三):把資料呈現在螢幕上
在 Android 中呈現 RSS 資料 此時,RSS 訂閱源裡的 XML 資料已安全地保存在記憶體中的 RSSFeed 實例裡,這個實例用一個便利的 List 結構裝著