49

我正在为大学做一个项目,让用户在地图上放置一个点,然后为覆盖对象设置标题和描述。问题是,第二个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);

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

4

4 回答 4

108

一个对话框只包含一个根视图,这就是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

(这是一个基本示例,但它应该可以帮助您入门。)

您应该注意 asetadd方法之间的命名差异。setView()仅持有一个 View,对于setMessage(). 事实上,这对于每种set方法都应该是正确的,你所想的是add命令。 add方法是累积的,它们会构建您推送的所有内容的列表,而set方法是单一的,它们会替换现有数据。

于 2012-10-13T20:28:11.953 回答
15

您可以构建包含两个 EditText 的布局,使用 a 对其进行膨胀并将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();

在此处输入图像描述

您也可以像这样以编程方式添加您的 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);
于 2012-10-13T20:51:34.763 回答
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()
于 2021-05-27T16:32:37.917 回答
-1

使用 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();

    }
于 2017-07-13T19:06:52.240 回答