AlertDialog中的多个EditText对象

时间:2012-10-13 20:18:24

标签: android google-maps dialog android-edittext

我正在开展一个大学项目,让用户在地图上放置一个点,然后设置叠加对象的标题和描述。问题是,第二个EditText框会覆盖第一个//Make new Dialog AlertDialog.Builder dialog = new AlertDialog.Builder(mapView.getContext()); dialog.setTitle("Set Target Title & Description"); dialog.setMessage("Title: "); final EditText titleBox = new EditText(mapView.getContext()); dialog.setView(titleBox); dialog.setMessage("Description: "); final EditText descriptionBox = new EditText(mapView.getContext()); dialog.setView(descriptionBox); 框。这是我的对话框代码。

{{1}}

任何帮助将不胜感激!谢谢!

4 个答案:

答案 0 :(得分:91)

对话框只包含一个根视图,这就是setView()覆盖第一个EditText的原因。解决方案很简单,将所有内容放在一个ViewGroup中,例如LinearLayout:

Context context = mapView.getContext();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);

// Add a TextView here for the "Title" label, as noted in the comments
final EditText titleBox = new EditText(context);
titleBox.setHint("Title");
layout.addView(titleBox); // Notice this is an add method

// Add another TextView here for the "Description" label
final EditText descriptionBox = new EditText(context);
descriptionBox.setHint("Description");
layout.addView(descriptionBox); // Another add method

dialog.setView(layout); // Again this is a set method, not add

(这是一个基本的例子,但它应该让你开始。)

您应该注意setadd方法之间的命名差异。 setView()只保留一个视图,setMessage()的视图相同。实际上,对于每个set方法都应该如此,您正在考虑的是add命令。 add方法是累积的,它们会在set方法为单数时构建一个列表,列出所有内容,它们会替换现有数据。

答案 1 :(得分:8)

您可以构建包含两个EditText的布局,使用LayoutInflater对其进行充气,并将其用作AlertDialog的视图。

LayoutInflater factory = LayoutInflater.from(this);

//text_entry is an Layout XML file containing two text field to display in alert dialog
final View textEntryView = factory.inflate(R.layout.text_entry, null);

final EditText input1 = (EditText) textEntryView.findViewById(R.id.EditText1);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.EditText2);


input1.setText("DefaultValue", TextView.BufferType.EDITABLE);
input2.setText("DefaultValue", TextView.BufferType.EDITABLE);

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.icon).setTitle("EntertheText:").setView(textEntryView).setPositiveButton("Save",
  new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,
     int whichButton) {

    Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
    Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
    /* User clicked OK so do some stuff */
   }
  }).setNegativeButton("Cancel",
  new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,
     int whichButton) {
     /*
     * User clicked cancel so do some stuff
     */
   }
  });
alert.show();

enter image description here

您可以通过编程方式添加EditText:

LinearLayout layout = new LinearLayout(mapView.getContext());
layout.setOrientation(LinearLayout.VERTICAL);

final EditText titleBox = new EditText(mapView.getContext());
titleBox.setHint("Title");
layout.addView(titleBox);

final EditText descriptionBox = new EditText(mapView.getContext());
descriptionBox.setHint("Description");
layout.addView(descriptionBox);

dialog.setView(layout);

答案 2 :(得分:0)

使用Xamarin

创建带有两个EditText的弹出窗口的代码
    public void dial()
    {

        AlertDialog alerta = new AlertDialog.Builder(this).Create();
        LinearLayout layout = new LinearLayout(this);

                   layout.Orientation = Orientation.Vertical;


        EditText factinput = new EditText(this);
        alerta.SetMessage("Facturas Disponibles:");
        layout.AddView(factinput);

        EditText canttinput = new EditText(this);
        alerta.SetMessage("Cantidad:");
        layout.AddView(canttinput);

        alerta.SetView(layout);


        alerta.SetButton("Cancelar", (a, b) =>
        {

            AlertDialog cencelacion = new AlertDialog.Builder(this).Create();
            cencelacion.SetMessage("Desea Cancelar");
            cencelacion.SetButton("OK", (c, d) => { });
            cencelacion.Show();

        });
        alerta.SetButton2("Aceptar", (ee, f) =>
        {
            AlertDialog confirmacion = new AlertDialog.Builder(this).Create();
            confirmacion.SetMessage("Realizar Busqueda de Factura");
            confirmacion.SetButton("OK", (c, d) => { });
            confirmacion.Show();
        }
        );

        alerta.Show();

    }

答案 3 :(得分:0)

val alert = AlertDialog.Builder(this)
    alert.setTitle("Buy Airtime")
    alert.setMessage("Enter phone details and amount to buy airtime.")

    val layout = LinearLayout(this)
    layout.orientation = LinearLayout.VERTICAL

    val mobileNoET = EditText(this)
    mobileNoET.setSingleLine()
    mobileNoET.hint = "Mobile Number"
    layout.addView(mobileNoET)

    val amountET = EditText(this)
    amountET.setSingleLine()
    amountET.hint = "Amount"
    layout.addView(amountET)

    val networkET = EditText(this)
    networkET.setSingleLine()
    networkET.hint = "Network"
    layout.addView(networkET)

    layout.setPadding(50, 40, 50, 10)

    alert.setView(layout)

    alert.setPositiveButton("Proceed") { _, _ ->
        val mobileNo = mobileNoET.text.toString()
        val amount = amountET.text.toString()
        val network = networkET.text.toString()

        Log.i("xxx",mobileNo )
        Log.i("xxx",amount )
        Log.i("xxx",network )

        Toast.makeText(this, "Saved Sucessfully", Toast.LENGTH_LONG).show()
    }

    alert.setNegativeButton("Cancel") { dialog, _ ->
        dialog.dismiss()
    }

    alert.setCancelable(false)
    alert.show()