Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

用 Eclipse 與 Android 元件做開發(三):TextView、EditText、Button 與 Toast 實作

編輯:開發入門

動手做一個用到元件的範例應用

現在來改寫現有的應用,讓它變成下圖那樣。這個例子的目的,是讓你熟悉幾個基本元件在真實應用中的用法: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

先在 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

接著用 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 定義完成,剩下就是讓活動程式碼接上它。

MainActivity.java

這個檔案負責把 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。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved