Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 兩層 AlertDialog:先選項目,再確認操作

Android 開發中 AlertDialog 幾乎無所不在。有一種常見需求是:先用列表形式讓使用者從多個選項中挑一個,接著再跳出確認框,詢問是否執行該操作。這種「先選再確認」的互動,用兩層 AlertDialog 串起來就能完成。

三個步驟

第一,在 XML 中用 <string-array> 定義要顯示的選項清單。
第二,在 Activity 中用 String[] items = getResources().getStringArray(R.array.item); 取出陣列。
第三,為按鈕加上點擊事件,以 AlertDialog.Builder 建立對話框,記得最後一定要呼叫 show(),否則畫面上什麼都不會出現。

原始碼

package com.jiangqq.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 使用 AlertDialog 進行選擇功能
 */
public class AlertDialogActivity extends Activity {

    private Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final String[] items = getResources().getStringArray(R.array.item);

                new AlertDialog.Builder(AlertDialogActivity.this)
                        .setTitle("請點擊選擇")
                        .setItems(items, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                // 第一層選完,接著開第二層做確認
                                new AlertDialog.Builder(AlertDialogActivity.this)
                                        .setTitle("你選擇了: " + items[which])
                                        .setMessage("點擊選擇操作")
                                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                // 點擊確定之後要執行的操作
                                            }
                                        })
                                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                // 點擊取消之後要執行的操作
                                            }
                                        }).show();
                            }
                        }).show();
            }
        });
    }
}

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, AlertDialogActivity!</string>
    <string name="app_name">Hello World, AlertDialogActivity</string>
    <string name="btn_name">點擊彈出AlertDialog</string>

    <string-array name="item">
        <item>第一個選擇</item>
        <item>第二個選擇</item>
        <item>第三個選擇</item>
        <item>第四個選擇</item>
    </string-array>

</resources>

幾個容易出錯的地方

用 setItems 而非 setSingleChoiceItems。前者點一下即關閉並回呼,正合「選完就確認」的流程;後者會保留選項的核取狀態,需要額外呼叫 setPositiveButton 才會結束,語意不同。

which 是索引。第一層回呼的 which 是被點選項目的索引,直接拿去索引 items 陣列即可拿到文字。第二層的 which 則是 0 代表確定、1 代表取消這類按鈕索引,兩者意義完全不同,不要混用。

記得 show()。Builder 只是組裝,不呼叫 show() 不會有任何畫面,這是新手最常卡住的一點。

若這段邏輯會被多處重複使用,把選項與回呼抽成獨立方法,或用 DialogFragment 承載,能避免 Activity 被回收後對話框洩漏視窗資源的經典錯誤。

Copyright © Linux教程網 All Rights Reserved