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

Android 對話框全攻略:普通、單選、多選、帶圖標、自定義 Adapter 與自定義 View

編輯:關於 Android 程式設計

對話框的本質就是一個 AlertDialog,但看似簡單的 AlertDialog,其實能玩出不少花樣。下面我們就一起把 AlertDialog 的各種用法系統性地梳理一遍,看看你在日常工作中是否都用上了這些特性。

普通對話框

普通對話框是最常用的類型,實現並不複雜,效果也最為樸素:

AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("普通對話框")
    .setIcon(R.drawable.a4s)
    .setNegativeButton("取消", null)
    .setPositiveButton("確定", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // 處理確定按鈕的點擊事件
        }
    })
    .setNeutralButton("中立", null)
    .setMessage("確認刪除?")
    .create();
dialog.show();

我們可以為對話框設置標題、圖標,並在底部放置取消、確定、中立三個按鈕,一般會在確定按鈕中加入相應的點擊事件。顯示效果如下:

顯示 Item 的對話框

若要在對話框中顯示若干個項目,可以用 setItems:

AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("顯示 Item 對話框")
    .setIcon(R.drawable.a4s)
    .setItems(items, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
        }
    }).create();
dialog.show();

其中 items 是一個字串陣列:

items = new String[] { "魏", "蜀", "吳" };

此時便不必再設置底部的三個按鈕了。onClick 方法中的 which 參數表示使用者點擊了第幾個項目,據此即可做出相應處理。顯示效果如下:

單選對話框

單選對話框同樣無需底部的三個按鈕:

AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("單選對話框")
    .setIcon(R.drawable.a4s)
    .setSingleChoiceItems(items, -1, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    }).create();
dialog.show();

setSingleChoiceItems 有三個參數:資料源、預選中的項目(-1 表示無預選)、以及事件處理方法。由於單選後需手動關閉,這裡多了一句 dialog.dismiss()。顯示效果如下:

多選對話框

與單選不同,多選對話框通常必須保留底部按鈕:

AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("多選對話框")
    .setIcon(R.drawable.a4s)
    .setNegativeButton("取消", null)
    .setPositiveButton("確定", null)
    .setMultiChoiceItems(items, null, new OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            // isChecked 表示該項是否被選中
        }
    }).create();
dialog.show();

setMultiChoiceItems 的第二個參數是一個 Boolean 陣列,表示初始時哪些項目被選中;點擊事件則多了 isChecked 參數,表示該項目是否被選中。我們可以用一個 boolean 陣列來記錄每個項目的選中情況,待使用者點擊確定後再統一取值。顯示效果如下:

自定義 Adapter

細心的朋友可能注意到,Builder 還有一個 setAdapter 方法,參數是一個 ListAdapter,也就是我們平時為 ListView 製作的 Adapter。能設置 Adapter,意味著 AlertDialog 能呈現的內容更加豐富多樣。這裡用最簡單的 ArrayAdapter 做個示範:

adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
    new String[] { "張三", "李四" });
AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("自定義 Adapter")
    .setIcon(R.drawable.a4s)
    .setAdapter(adapter, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    }).create();
dialog.show();

顯示效果如下:

有了 Adapter,我們想讓 AlertDialog 呈現什麼樣子,就能呈現什麼樣子,靈活性極大。

自定義 View

而終極大 Boss 還不是自定義 Adapter,而是自定義 View。透過自定義 View,我們可以讓 AlertDialog 顯示出任何想要的介面:

View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("自定義 View")
    .setIcon(R.drawable.a4s)
    .setView(view)
    .create();
dialog.show();

顯示效果如下:

使用自定義 View 的方式,我們可以打造出形形色色的 Dialog,比如登錄框、關於或說明框等等。AlertDialog 的用法,基本上就這麼幾種。

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