0

当用户想要添加新项目时,我调用了一个侦听器,侦听器应该以这种形式显示一个带有 2 EditText 的 AlertDialog

-AlertDialog Title
-AlertDialog Message
-TextView 1 above EditText1
-EditText1
-TextView 2 above EditText2
-EditText 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 和两个按钮,但我得到了一个 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 回答
1

您可以通过AlertDialog此处创建 对话框myalertdialog.xml的布局来显示您的custom dialog

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+@id/myLayout
android:layout_width="match_parent"
android:layout_hight="match_parent"
android:orientation="vertical">
<TextView
android:id="+@id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView1"/>
<EditText
android:id="+@id=myeditText1'
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="+@id/myTextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView2"/>
<EditText
android:id="+@id=myeditText2'
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

然后在你的代码里面

AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this);
LayoutInflator inflater=getLayoutInflator();
View dialogView=inflator.inflate(R.layougt.myalertdialog,(ViewGroup) getCurrentFocus());
dialogView.setView(dialogView);
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) {
                        }
                    });
dialog=builder.create();
dialog.show();
于 2013-07-24T18:16:08.820 回答
1

通过这里的链接

XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical"  >

<TextView
    android:id="@+id/lbl_your_name" android:text="Your name" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" />

<EditText
    android:id="@+id/txt_your_name"
    android:layout_width="match_parent"  android:layout_height="wrap_content" 
    android:inputType=”text”
    android:imeOptions="actionDone" />
</LinearLayout>

文件

public class EditNameDialog extends DialogFragment implements OnEditorActionListener {

public interface EditNameDialogListener {
    void onFinishEditDialog(String inputText);
}

private EditText mEditText;

public EditNameDialog() {
    // Empty constructor required for DialogFragment
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit_name, container);
    mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    getDialog().setTitle("Hello");

    // Show soft keyboard automatically
    mEditText.requestFocus();
    getDialog().getWindow().setSoftInputMode(
            LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    mEditText.setOnEditorActionListener(this);

    return view;
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (EditorInfo.IME_ACTION_DONE == actionId) {
        // Return input text to activity
        EditNameDialogListener activity = (EditNameDialogListener) getActivity();
        activity.onFinishEditDialog(mEditText.getText().toString());
        this.dismiss();
        return true;
    }
    return false;
}
}
于 2013-07-24T18:20:56.023 回答
0

你为什么不为你的警报制作一个单独的 xml 布局......问题出在 alert.setView(view); setView(view) 和 addView(view) 有区别;

于 2013-07-24T17:39:15.843 回答