0

我正在编写我的第一个 Android 应用程序,并且我有一个要显示两个单独对话框的活动:

对话框 A - 一个简单的 AlertDialog,显示文本并被关闭 对话框 B - 带有 EditText 和保存和取消按钮的“另存为”弹出窗口

我找到了有关创建 AlertDialogs 和自定义对话框的教程,并且我已经能够让它们中的每一个工作,但只能单独工作。当我尝试将所有代码放入 onCreateDialog 方法中的 switch/case 语句时,启动 AlertDialog 时应用程序崩溃。

这是我的 onCreateDialog 代码:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        // Display dialog
        case 0:  
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setMessage(messageText);
            alertDialog.setNegativeButton(android.R.string.ok, 
                    new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                }
            });
            return alertDialog.create();    
        // Save As dialog

        case 1: 
            Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.save_as);
            dialog.setTitle("Save as:");

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

                public void onClick(View v) {

                }

            });
            return dialog;  
        }
        return null;

    }

任何一种情况都可以自行工作,但是当我将两种情况都放入时,应用程序会崩溃。

这是自定义对话框布局的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:padding="10dp">

    <TextView 
        android:text="Save this list as:"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <EditText 
        android:id="@+id/list_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:drawable/bottom_bar" 
    android:paddingLeft="4.0dip"
    android:paddingTop="5.0dip" 
    android:paddingRight="4.0dip"
    android:paddingBottom="1.0dip" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
        <Button
           android:id="@+id/save" 
           android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Save"
        android:layout_weight="1.0"
            ></Button>
        <Button
            android:id="@+id/cancel" 
            android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Cancel"
        android:layout_weight="1.0"
            ></Button>

    </LinearLayout>

</LinearLayout>

我应该坚持使用一种格式还是另一种格式?我还读到现在首选 DialogFragments,但我还没有找到任何好的新手级教程。任何建议将不胜感激。

4

1 回答 1

0

我最终意识到我需要在对话框中传入和传出数据,并且我的目标是低 API,所以我只是将 Save As 对话框更改为 Activity,一切正常。一路上学到了很多对话的限制......

于 2012-02-09T00:03:04.897 回答