編輯:關於 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 resource,讓背景依按鈕狀態改變。步驟如下:
<?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 時才會套用。
<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 的官方文件。
實作可調透明度的 Android 圖片檢視器與局部放大
這個實例做了一個功能不算多但相當實用的圖片檢視器:可以切換圖片、以按鈕調整透明度,而且手指觸碰主圖時,另一個 ImageView 會即時顯示觸碰位置附近的局部放大——有點
Android 使用 AlertDialog 實作消息彈窗
在 Android 應用中,當需要提醒使用者進行某項操作或確認某個決定時,常會使用對話框。AlertDialog 是最常見的選擇之一,可用來顯示標題、內容與多個操作按鈕。
深入理解 Android findViewById 的查找原理
從 Activity 的 findViewById 說起 在 Android 開發中,findViewById 幾乎每天都會用到。它看似只是依 id 取得 View,但實
Android AutoCompleteTextView:自動提示輸入框的資料來源與設定
自動提示輸入框(AutoCompleteTextView)能明顯改善使用者體驗、縮短輸入時間——百度搜尋框就是最典型的例子。它的資料來源有兩種常見做法:從 XML 資源陣