編輯:關於 Android 程式設計
本文示範如何在 Android 應用中建立一個簡單的 Button 或 ImageButton,從加入版面配置開始,接著說明兩種處理點擊事件的方式,最後補充這類控制項的其他可用特性。
先照平常的方式把 Android 專案建好並確認能執行,然後決定要把按鈕放在哪個畫面上。多數教學會直接使用預設的 Activity 與 main.xml 版面,本文也以此為例。
Android SDK 提供兩個簡單的按鈕控制項:Button(android.widget.Button)與 ImageButton(android.widget.ImageButton)。兩者功能相近,差別主要在外觀——Button 顯示文字標籤,ImageButton 則用一張可繪物件取代文字。
Button 的典型場景是「儲存」這類帶文字說明的動作;ImageButton 則適合音樂播放器的控制列,例如播放、暫停、停止等圖示化操作。
下圖左為 Button,右為 ImageButton:

SDK 中還有幾個較少被提及、但同樣繼承自這兩種基本按鈕的類別,包括 CompoundButton、RadioButton、ToggleButton 與 ZoomButton。若需要更特殊的行為,也可以繼承合適的類別來自製控件。
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>
此時執行專案,按鈕會出現,但點下去沒有反應。最簡單的做法是利用 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();
}
按下按鈕後就會跳出提示訊息:

另一種做法是呼叫 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
Android 修改 EditText 下劃線顏色的做法
前言 使用 AppCompatEditText(即 Layout 中的 EditText)時,預設的下劃線會跟隨主題的 colorControlNormal 與 colo
Android 自訂 View 打造支付寶風格六位密碼輸入元件
這個範例會實作一個類似支付寶的六位密碼輸入畫面。整體做法是透過 PopupWindow 顯示自訂的 PayView,當使用者輸入完六位數字後,再由回呼把結果傳回 Acti
Android 重力感應實作示例:讀取加速度感測器三軸數值
本文以實例講解 Android 中重力感應效果的實現,供大家參考。 Android 上許多遊戲都運用了重力感應技術,於是便深入研究了一下。我們以螢幕的左下方為原點,箭頭指
Android App 耗電優化實戰指南
可能造成耗電的一些原因 網絡請求耗電,而且用手機數據網絡發 HTTP 請求比用無線網更耗電,因為數據網絡會調用一些底層硬件模塊,就像 GPS 一樣,一旦開啟就會啟動硬件模