Android Studio添加设置2在同一个警报对话框中编辑文本

时间:2017-12-01 02:28:15

标签: java android

我正在尝试向此警报对话框添加第二个编辑文本。我使用了.setView()但它只会显示一个或另一个。如何在警报对话框中显示它们?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_add_task:
            final EditText taskEditText = new EditText(this);
            final EditText priorityEditText = new EditText(this);
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("Add New Task")
                    .setMessage("what do you want to do next")
                    .setView(priorityEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String task = String.valueOf(taskEditText.getText());
                            String priority = String.valueOf(priorityEditText.getText());
                            dbHelper.insertNewTask(task, priority);
                            loadTaskList();
                        }
                    })
                    .setNegativeButton("Cancel",null)
                    .create();
            dialog.show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:0)

1.您添加LinearLayout

2.将EditText添加到LinearLayout。并设置linearLayout.setOrientation(LinearLayout.VERTICAL);

3.使用setView(linearLayout)添加LinearLayout

你可以这样做。

final EditText taskEditText = new EditText(this);
final EditText priorityEditText = new EditText(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(taskEditText);
linearLayout.addView(priorityEditText);
AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Add New Task")
        .setMessage("what do you want to do next")
        .setView(linearLayout)
        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String task = String.valueOf(taskEditText.getText());
                String priority = String.valueOf(priorityEditText.getText());
            }
        })
        .setNegativeButton("Cancel", null)
        .create();
dialog.show();

<强>输出

enter image description here