Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Developers:按鈕(Button)完整用法教學

編輯:關於 Android 程式設計

在 Android 中建立按鈕,可以依需求選擇三種方式:純文字按鈕、純圖示按鈕,或文字加圖示的組合。以下依序說明如何建立、如何回應點擊事件,以及如何設計與自訂按鈕外觀。

一、建立按鈕的三種方式

使用 Button 類別設定文字:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    ... />

使用 ImageButton 類別設定圖示:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_icon"
    ... />

使用 Button 搭配 android:drawableLeft 屬性,同時顯示文字與圖示:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

二、回應點擊事件

當使用者點擊按鈕時,按鈕物件會觸發一個 on-click 事件。定義事件處理程序有兩種方式。

方式一:在 XML 布局中用 android:onClick。屬性值必須與要呼叫的方法名稱相同,且顯示該布局的 Activity 必須實作這個方法:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />
/** Called when the user touches the button */
public void sendMessage(View view) {
    // Do something in response to button click
}

這個方法必須符合三個條件:宣告為 public、回傳 void,並且定義一個 View 物件作為唯一參數(即被點擊的 View)。

方式二:使用 OnClickListener。若你在執行期才建立 Button 物件,或需要在 Fragment 子類中宣告點擊行為,就適合用這種程式化寫法:

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

三、設計你的按鈕

按鈕的外觀(背景圖與文字樣式)可能因裝置製造商而異,因為各家通常有不同的預設控制項風格。你可以透過主題統一控制樣式,例如在 Manifest 的 <application> 元素中宣告 android:theme="@android:style/Theme.Holo",確保 Android 4.0 以上裝置使用 Holo 主題。若想自訂背景,可用 Drawable 或 Color 資源指定 android:background,或改用 style 一次定義背景、字型、大小等多項屬性。

四、無邊框按鈕

無邊框按鈕沒有邊框或背景,但會在不同狀態(如被點擊時)改變顯示。只要套用 borderlessButtonStyle 屬性即可:

<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    style="?android:attr/borderlessButtonStyle" />

五、自訂背景(State List Drawable)

若想徹底掌控按鈕外觀,建議使用 state list resource,讓背景依按鈕狀態改變。步驟如下:

  1. 為按鈕準備三張 Nine-patch 位圖,分別代表 default、pressed、focused 狀態,命名為 button_default.9.png、button_pressed.9.png、button_focused.9.png。
  2. 把這些位圖放到專案的 res/drawable 目錄。
  3. 在 res/drawable/ 下建立一個 xml 檔(如 button_custom.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

第一個 item 定義被點擊時的位圖,第二個定義取得焦點時的位圖(例如用方向鍵或滾球高亮時),第三個則是預設狀態。注意 item 的順序很重要:系統會依序比對狀態,因為預設位圖放在最後,只有當 pressed 與 focused 都為 false 時才會套用。

  1. 最後把這個 drawable 套用為按鈕背景:
<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    android:background="@drawable/button_custom" />

若需要定義 disabled 或其他更多狀態,可參考 State List Drawable 的官方文件。

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