0

私は最初の Android アプリケーションを作成しており、2 つの別々のダイアログを表示したいアクティビティが 1 つあります。

ダイアログ A - テキストを表示して閉じられる単純な AlertDialog ダイアログ B - EditText と [保存] ボタンと [キャンセル] ボタンを含む [名前を付けて保存] ポップアップ

AlertDialogs と Custom Dialogs の作成に関するチュートリアルを見つけました。それぞれを機能させることができましたが、個別にしか機能しませんでした。すべてのコードを 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をターゲットにしているため、[名前を付けて保存]ダイアログをアクティビティに変更しただけで、すべて正常に機能します。しかし、途中でダイアログの多くの制限を学びました。

于 2012-02-09T00:03:04.897 に答える