0

ユーザーが新しいアイテムを追加したいときに呼び出されるリスナーがあります。リスナーは、このフォームで 2 つの EditText を含む AlertDialog を表示する必要があります。

-AlertDialog Title
-AlertDialog Message
-TextView 1 above EditText1
-EditText1
-TextView 2 above EditText2
-EditText 2

2 つのボタン

-*Cancel* on the left
-*Add* on the right

私のコードは

OnClickListener addNewItemListener = new OnClickListener() {
        public void onClick(View v) {
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    MyActivity.this);
            alert.setTitle(R.string.add_title);
            alert.setMessage(R.string.add_message);
                    final TextView t1 = new TextView(MyActivity.this);
                      t1.setText("Name");
            final EditText input1 = new EditText(MyActivity.this);
                    final TextView t2 = new TextView(MyActivity.this);
                    t2.setText("Value");
            final EditText input2 = new EditText(MyActivity.this);
                    alert.setView(t1);
            alert.setView(input1);
                    alert.setView(t2);
            alert.setView(input2);
            alert.setPositiveButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                        }
                    });
            alert.setNegativeButton(R.string.add,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            try {
                                String name = input1.getText().toString();
                                double value = Double.parseDouble(input2
                                        .getText().toString());
                                addItem(name, value);
                            } catch (RuntimeException e) {
                                Alerts.DatiErrati(MyActivity.this);
                            }

                        }
                    });
            alert.show();
        }
    };

残念ながら、当初の意図にもかかわらず、EditTextと2つのボタンしかないAlertDialogが表示されます

どうすればこれを修正できますか?

4

4 に答える 4

1

LinearLayout に複数のウィジェットを含め、最終的なレイアウトに setView を使用する必要があります

これを試して

OnClickListener addNewItemListener = new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        MyActivity.this);

                LinearLayout myLayout= new LinearLayout(MyActivity.this);
                myLayout.setOrientation(LinearLayout.VERTICAL);

                alert.setTitle(R.string.add_title);
                alert.setMessage(R.string.add_message);

                final TextView t1 = new TextView(MyActivity.this);
                t1.setText("Name");
                final EditText input1 = new EditText(MyActivity.this);
                final TextView t2 = new TextView(MyActivity.this);
                t2.setText("Value");
                final EditText input2 = new EditText(MyActivity.this);
                myLayout.addView(t1);
                myLayout.addView(input1);
                myLayout.addView(t2);
                myLayout.addView(input2);
                alert.setView(myLayout);
                alert.setPositiveButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                            }
                        });
                alert.setNegativeButton(R.string.add,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                try {
                                    String name = input1.getText().toString();
                                    double value = Double.parseDouble(input2
                                            .getText().toString());
                                    addItem(name, value);
                                } catch (RuntimeException e) {
                                    Alerts.DatiErrati(MyActivity.this);
                                }

                            }
                        });
                alert.show();
            }
        };
于 2013-07-24T18:10:09.803 に答える