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

Android 新手入門 2016(13)——阻塞對話框 PopupWindow

編輯:關於 Android 開發

Android 新手入門 2016(13)——阻塞對話框 PopupWindow


前兩章講的都是非阻塞的對話框,今天來說明阻塞式的對話框——PopupWindow。

那麼什麼是阻塞、什麼是非阻塞?PopupWindow 和 AlertDialog 又有何不同?先說 AlertDialog:彈出之後背景會變灰,並不會阻塞後台進程;若無特殊控制,點擊後方灰暗處,彈框就會消失。

至於 PopupWindow,彈出後背景沒有任何變化,並且會阻塞該應用的進程——若一直不退出,應用就會持續等待,此時點擊後方也不會有反應。

由於圖片暫時無法上傳,這裡就不附圖了,其實用法與 AlertDialog 類似。以下繼續沿用之前的程式碼進行擴充。

首先新增了一個 popup_window.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"
    android:background="#FFFFFF">
    <TextView
        android:id="@+id/data_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="我要大保健">
    </TextView>
    <EditText
        android:id="@+id/data_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/BtnOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="100"
            android:text="確定">
        </Button>
        <Button
            android:id="@+id/BtnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="100"
            android:text="取消">
        </Button>
    </LinearLayout>
</LinearLayout>

這裡聲明了一個標題、一個輸入框與兩個按鈕。接著看實作程式碼(看過 AlertDialog 的可以跳到下面的程式碼):

package com.fable.helloworld;

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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import java.util.*;

public class HelloWorldActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
        // 設定主佈局檔
        GridView gridview = (GridView) findViewById(R.id.gridview);
        // 建立資料來源
        ArrayList<HashMap<String, Object>> images = new ArrayList<HashMap<String, Object>>();
        for (int i = 1; i < 10; i++) {
            String imageName = "";
            switch (i) {
                case 1: imageName = "AlertDialog"; break;   // 普通的 AlertDialog
                case 2: imageName = "AlertDialog2"; break;  // 基於佈局的 AlertDialog
                case 3: imageName = "PopupWindow"; break;   // 阻塞對話框
                default: imageName = "app" + String.valueOf(i);
            }
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.ic_launcher);
            map.put("ItemText", imageName);
            images.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this, images, R.layout.my_list_item,
                new String[] {"ItemImage","ItemText"},
                new int[] {R.id.ItemImage, R.id.ItemText});
        gridview.setAdapter(simpleAdapter);
        gridview.setOnItemClickListener(new ItemClickListener());
    }

    class ItemClickListener implements OnItemClickListener {
        public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
            HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
            String itemStr = (String) item.get("ItemText");
            if (itemStr.equals("AlertDialog")) {
                showDialog(HelloWorldActivity.this, itemStr);
            } else if (itemStr.equals("AlertDialog2")) {
                showDialogLayout(HelloWorldActivity.this);
            } else if (itemStr.equals("PopupWindow")) {
                showPopupWindow(HelloWorldActivity.this, arg1);
            }
        }

        // ===================== AlertDialog =====================
        private void showDialog(Context context, String itemStr) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle("我是標題");
            builder.setMessage("這裡是內容啊啊啊啊!!!");
            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 showDialogLayout(Context context) {
            // LayoutInflater 用於動態載入 Layout 檔
            LayoutInflater inflater = LayoutInflater.from(context);
            final View textEntryView = inflater.inflate(R.layout.dialog_layout, 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.ic_launcher);
            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();
        }

        // ===================== PopupWindow =====================
        private void showPopupWindow(Context context, View parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            final View popupView = inflater.inflate(R.layout.popup_window, null);
            final PopupWindow pWindow = new PopupWindow(popupView, 200, 200, true); // 必須指定寬高,否則無法顯示
            final Button button = (Button) popupView.findViewById(R.id.BtnOK);
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText edtUsername = (EditText) popupView.findViewById(R.id.data_edit);
                    edtUsername.setText("關注微信:傳說之路");
                }
            });
            // Cancel 按鈕及其事件
            Button btnCancel = (Button) popupView.findViewById(R.id.BtnCancel);
            btnCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pWindow.dismiss(); // 關閉
                }
            });
            // 顯示 PopupWindow 對話框
            pWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
        }
    }
}

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