編輯:Android 開發實例
隔了差不多一年又回到 Android 開發,趁機把知識重新整理一遍。這次的主題是 AlertDialog,實務上最常遇到的對話框形式。
要特別注意的是,AlertDialog 與 Win32 開發中的 Dialog 概念不同:AlertDialog 是非阻塞的,呼叫 show() 之後程式立刻往下走,後續反應得靠回呼處理;真正阻塞式的彈出元件是 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>
最簡單的用法是靠 AlertDialog.Builder 串起標題、圖示、訊息與按鈕。這裡示範了三顆按鈕:setPositiveButton、setNeutralButton、setNegativeButton,分別對應通常被理解為「確認」「中立」「取消」的角色。每顆按鈕各自帶一個 DialogInterface.OnClickListener,按下後把結果寫回 Activity 標題,方便觀察事件流向。
若對話框需要承載輸入框或其他元件,就得自己準備一份版面。dialoglayout.xml 只放了一個 EditText,重點在於怎麼把它接進對話框:LayoutInflater.from(this).inflate(R.layout.dialoglayout, null) 產生 View 之後,交給 builder.setView() 即可。同時用 setCancelable(false) 禁止使用者靠返回鍵關閉,強制作出選擇。
要讀取輸入內容,只要在按鈕回呼裡對那個 EditText 呼叫 getText(),因為它是同一份 View 樹上的物件,參照取得即可,不需要另外傳值。
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.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupWindow;
- public class testAlertDialog extends Activity {
- Button btnShowDialog;
- Button btnShowDialog_Layout;
- /** Called when the activity is first created. */
- @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) {
- // TODO Auto-generated method stub
- 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();
- }
- }
Android 教學:用 RadioButton 實作圖文上下或左右排列
在 Android 開發中,經常會需要用到同時帶有圖片與文字的按鈕。本文就來示範如何利用 RadioButton,輕鬆做出圖片與文字「上下排列」或「左右排列」的效果。程式
Android XML 解析實戰:SAX 機制與 ContentHandler 的使用
Android 中解析 XML 主要有兩條路:DOM 與 SAX。 DOM 需要先把整個文檔讀入內存並構建樹結構,操作直觀,但文檔過大時內存吃不消。SAX 則是逐行流式解
Android 錄音實作:MediaRecorder 搭配 MediaPlayer 完整流程
幾乎每支 Android 手機都內建麥克風,錄下聲音後存檔或播放都不是難事。做法有好幾種,最常見的是 MediaRecorder 類別;錄完之後再用 MediaPlaye
ListView 分頁加載實作:滚动到底自動載入更多資料
ListView 是 Android 最常用的控件之一。實務上如果一次把幾千筆資料塞進去,畫面會卡、記憶體會爆,因此分頁加載幾乎是必備技巧。 本範例把「載入更多」的觸發點