編輯:關於 Android 程式設計
本文談 AlertDialog,這是開發中最常遇到的對話框。要特別注意的是,它和 Win32 開發裡的 Dialog 概念不同——AlertDialog 是非阻塞的,彈出之後程式繼續往下跑;真正阻塞的對話框是 PopupWindow。
先看程式執行截圖:

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" > <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:text="非Layout型對話框" android:layout_width="fill_parent"></Button> <Button android:id="@+id/Button02" android:layout_height="wrap_content" android:text="Layout型對話框" android:layout_width="fill_parent"></Button><View android:id="@+id/View01" android:layout_width="wrap_content" android:layout_height="wrap_content"></View> </LinearLayout>
下圖是非 Layout 型對話框,直接用 AlertDialog 的 Builder 組合出圖示、標題、訊息與按鈕:

下圖則是套用了自訂 Layout 的對話框,可以放入任意控件,實現更複雜的互動:

dialoglayout.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/edtInput"/> </LinearLayout>
程式碼:
package com.testAlertDialog; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class testAlertDialog extends Activity { Button btnShowDialog; Button btnShowDialog_Layout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //定義按鈕 btnShowDialog=(Button)this.findViewById(R.id.Button01); btnShowDialog.setOnClickListener(new ClickEvent()); btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02); btnShowDialog_Layout.setOnClickListener(new ClickEvent()); } //統一處理按鍵事件 class ClickEvent implements OnClickListener{ @Override public void onClick(View v) { if(v==btnShowDialog) showDialog(testAlertDialog.this); else if(v==btnShowDialog_Layout) showDialog_Layout(testAlertDialog.this); } } //顯示基本的AlertDialog private void showDialog(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.drawable.icon); builder.setTitle("Title"); builder.setMessage("Message"); builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("點擊了對話框上的Button1"); } }); builder.setNeutralButton("Button2", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("點擊了對話框上的Button2"); } }); builder.setNegativeButton("Button3", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("點擊了對話框上的Button3"); } }); builder.show(); } //顯示基於Layout的AlertDialog private void showDialog_Layout(Context context) { LayoutInflater inflater = LayoutInflater.from(this); final View textEntryView = inflater.inflate( R.layout.dialoglayout, null); final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput); final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setCancelable(false); builder.setIcon(R.drawable.icon); builder.setTitle("Title"); builder.setView(textEntryView); builder.setPositiveButton("確認", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(edtInput.getText()); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(""); } }); builder.show(); } }
兩種形態的選擇標準很清楚:只是要告知或做簡單選擇,用 setMessage 加三個按鈕就夠了;需要使用者輸入或呈現自訂內容,就靠 setView() 塞進自己的 Layout。另外別忘了 setCancelable(false) 的作用——強制使用者做出選擇,不給返回鍵關閉的機會。
Android 以 TabHost(Tabs3)實作仿優酷分頁效果
本文分享如何以 Android 內建的 TabHost(參考 API Demos 中的 Tabs3)實作一個仿優酷影片客戶端的分頁效果,供需要的朋友參考。 前陣子老師要求
Android 透過 JSON 將資料寫入 MySQL 的完整實作(寫入篇)
上一篇講了讀取,這篇來談寫入。Android 把本地資料(例如定位、感測器讀數)上傳到資料庫,流程是:把參數封裝成 POST 表單 → 送到 PHP → PHP 寫入 M
Android 仿微信朋友圈點讚與評論彈出框實作
微信朋友圈的點讚與評論互動,是許多社群型介面會參考的模式。本文以 ListView 為基礎,示範如何做出類似效果:點擊更多按鈕後彈出操作框,選擇評論後開啟輸入區,並把新的
Android 自訂驗證碼控件:Canvas 繪製實作
這篇文章會實作一個簡單的驗證碼 View,使用 Canvas 繪製背景、干擾線、隨機點與驗證碼文字。點擊驗證碼時,會重新產生一組新的驗證碼。 實作思路 建立自訂 Vi