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

Android 介面設計入門:Button 與 ImageButton 怎麼用

編輯:關於 Android 程式設計

本文示範如何在 Android 應用中建立一個簡單的 Button 或 ImageButton,從加入版面配置開始,接著說明兩種處理點擊事件的方式,最後補充這類控制項的其他可用特性。

第 1 步:建立專案

先照平常的方式把 Android 專案建好並確認能執行,然後決定要把按鈕放在哪個畫面上。多數教學會直接使用預設的 Activity 與 main.xml 版面,本文也以此為例。

第 2 步:認識 Button 與 ImageButton

Android SDK 提供兩個簡單的按鈕控制項:Button(android.widget.Button)與 ImageButton(android.widget.ImageButton)。兩者功能相近,差別主要在外觀——Button 顯示文字標籤,ImageButton 則用一張可繪物件取代文字。

Button 的典型場景是「儲存」這類帶文字說明的動作;ImageButton 則適合音樂播放器的控制列,例如播放、暫停、停止等圖示化操作。

下圖左為 Button,右為 ImageButton:

SDK 中還有幾個較少被提及、但同樣繼承自這兩種基本按鈕的類別,包括 CompoundButton、RadioButton、ToggleButton 與 ZoomButton。若需要更特殊的行為,也可以繼承合適的類別來自製控件。

第 3 步:把按鈕加入版面

Button 通常是 Activity 版面資源的一部分。要加到 main.xml,你可以用 Eclipse 的版面設計器,也可以直接編輯 XML。按鈕同樣能以程式碼動態建立並在執行期加入畫面。

開啟 /res/layout/main.xml,選取要放入按鈕的 LinearLayout(或 RelativeLayout、FrameLayout 等父層容器),在 Outline 面板點選父層配置後,用綠色加號新增控制項,選擇 Button 即可。

接著在屬性面板調整外觀,幾個常用屬性如下:

· id:給按鈕一個唯一名稱;
· text:設定 Button 上要顯示的文字;
· src:設定 ImageButton 上要顯示的圖片;
· layout_height 與 layout_width:一般設為 wrap_content;
· textColor、textSize、textStyle 等:調整字型外觀。

以下版面資源對應前面那張截圖:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">
    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:minHeight="92dp"
        android:textSize="22dp"
        android:onClick="onMyButtonClick"></Button>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/skater"
        android:id="@+id/ImageButton01"
        android:layout_toRightOf="@+id/Button01"></ImageButton>
</RelativeLayout>

第 4 步:處理點擊(方式一:onClick 屬性)

此時執行專案,按鈕會出現,但點下去沒有反應。最簡單的做法是利用 Button 與 ImageButton 都有的 onClick 屬性(屬性面板顯示為「On Click」),把要處理點擊的方法名稱填進去,再到 Activity 中實作該方法:

android:onClick="onMyButtonClick"

這個方法必須是 public、回傳 void,且帶一個 View 參數:

public void onMyButtonClick(View view) {
    Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();
}

按下按鈕後就會跳出提示訊息:

第 5 步:處理點擊(方式二:實作 OnClickListener)

另一種做法是呼叫 setOnClickListener() 註冊一個 View.OnClickListener。它取代了「在版面中指定方法名」的方式,改以程式碼動態掛上監聽器。雖然看起來多寫了幾行,但理解它很重要——點擊往往不是唯一需要處理的事件,長按就是常見的例子。

通常在 Activity 的 onCreate() 中完成:先用 findViewById() 抓到控制項,再呼叫 setOnClickListener(),最後實作 onClick():

ImageButton myImageButton = (ImageButton) findViewById(R.id.ImageButton01);
myImageButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(BasicButtonActivity.this, "ImageButton clicked!",
                Toast.LENGTH_SHORT).show();
    }
});

同样的手法搭配 setOnLongClickListener(),就能處理長按事件。

總結

按鈕是 Android 應用中最常出現的元件之一。本文示範了 Button 與 ImageButton 兩種控制項的建立,以及實作點擊回應的兩條路徑:版面中的 onClick 屬性,以及程式碼裡的 setOnClickListener()。前者簡潔、適合單純場景;後者靈活、便于統一管理多種事件,可依專案規模自行取捨。

原文作者:RockUX 原文連結:https://mobile.51cto.com/design-254382.htm

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