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

Android 如何用 PopupWindow 打造對話框(附完整可運行範例)

編輯:關於 Android 程式設計

不少剛接觸 Android 的朋友會疑惑:系統裡明明有 AlertDialog,為什麼還要自己折騰?原因很簡單——Android 並沒有把「對話框」當成一個獨立的一等概念,真正承擔這份工作的,是一個名叫 PopupWindow 的浮層容器。它可以在任意 View 之上彈出一塊自訂內容,樣式、背景、按鈕排列完全由你決定,因此實現出來的視覺效果往往比系統預設的提示框更貼合產品設計。

下面我用一個「註冊提示」的登陸彈窗為例,把整個流程走一遍:自訂類、佈局檔、以及最關鍵的呼叫方式。

第一步:建立 PromptWindow 類

讓自訂類繼承 PopupWindow,在構造函式裡完成三件事:加載佈局、綁定按鈕事件、設定視窗尺寸與背景。這樣做的好處是把彈窗的「樣子」和「行為」全部封裝起來,Activity 那邊只需要一行呼叫。

package com.friendlocation;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class PromptWindow extends PopupWindow {

    Activity context;
    View popupView;
    OnBtnClickedListener listener;

    public PromptWindow(Activity context, int width, int height) {
        super(context);
        this.context = context;

        // 載入彈窗佈局
        popupView = context.getLayoutInflater().inflate(R.layout.prompt_window, null);

        Button btnOk = (Button) popupView.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (listener != null) listener.onBtnOkClicked(PromptWindow.this);
            }
        });

        // 顯示對話框
        this.setContentView(popupView);
        this.setWidth(width);
        this.setHeight(height);
        setFocusable(true);
        setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_menu));
    }

    public interface OnBtnClickedListener {
        public void onBtnOkClicked(PopupWindow win);
    }

    public void setOnBtnClickedListener(OnBtnClickedListener l) {
        listener = l;
    }
}

這裡有兩個容易踩坑的細節:一是 setFocusable(true),設為 true 之後彈窗才會攔截返回鍵與觸摸事件;二是 setBackgroundDrawable(),如果不給背景,彈窗以外的區域會無法點擊關閉。

第二步:在 res/layout 下新增 prompt_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_menu_recent"
    android:orientation="vertical"
    android:padding="2dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#333333"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/menu_title"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#555555"
            android:gravity="center"
            android:text="注冊提示"
            android:textColor="#EEEEEE"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:padding="4dp"
            android:text="軟件未注冊時限制最多100條信息,請刪除若干消息後繼續使用。或購買注冊碼。"
            android:textColor="#DDDDDD"
            android:textSize="14sp" />

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="100dp"
            android:layout_height="28dp"
            android:layout_gravity="right"
            android:layout_margin="4dp"
            android:background="@drawable/toolbutton"
            android:text="點此注冊"
            android:textColor="#DDDDDD" />
    </LinearLayout>
</LinearLayout>

第三步:在 Activity 中彈出對話框

PromptWindow win = new PromptWindow(this, 420, 240);
win.showAtLocation(this.findViewById(R.id.titlebar), Gravity.CENTER, 0, 0);

// 點擊按鈕之後要做的事
win.setOnBtnClickedListener(new PromptWindow.OnBtnClickedListener() {
    public void onBtnOkClicked(PopupWindow w) {
        w.dismiss();
        // Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
        // startActivityForResult(intent, 100);
    }
});

showAtLocation() 的第一個參數是錨點 View,最後兩個參數是相對於錨點的偏移量;搭配 Gravity.CENTER 就能讓彈窗置中顯示。記得在離開時呼叫 dismiss(),否則視窗會洩漏。

------------------------------------------------------------------------------------------------------------------

「極星親友定位」是一款簡單實用的定位軟體,適合家人與朋友之間互相掌握行蹤,查詢對方位置後直接用地圖呈現。少一份擔心,多一份安心。

它最實用的地方在於「對方什麼都不用做」:透過白名單自動回覆機制,即使長輩不熟悉智慧型手機操作、也不需要事先開啟軟體,你就能取得對方位置。

下載位置:360 手機助手(搜尋關鍵詞「極星」或「極星親友定位」)

- 雙方安裝並開啟 GPS 後,輸入對方手機號即可查詢位置。
- 支援從通訊錄或「最近聯絡人」中直接挑選對象。
- 白名單用於指定受信任的聯絡人,對他們的查詢請求會自動回覆。
- 若手機已安裝百度地圖,可直接在地圖上標示對方位置。
- 傳送按鈕採延時確認設計,需按住約一秒才送出,並以震動與文字提示,避免誤觸。

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