AlertDialog中的多个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); 

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

Solutions Collecting From Web of "AlertDialog中的多个EditText对象"

就像任何其他的布局,你需要添加两个EditTexts到一个ViewGroup:

 Context context = mapView.getContext(); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.VERTICAL); final EditText titleBox = new EditText(context); titleBox.setHint("Title"); layout.addView(titleBox); final EditText descriptionBox = new EditText(context); descriptionBox.setHint("Description"); layout.addView(descriptionBox); dialog.setView(layout); 

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

您可以构build包含两个EditText的布局,使用LayoutInflater对其进行膨胀,并将其LayoutInflater的视图。

 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); 

使用Xamarin创build一个带有两个EditText的popup窗口的代码

  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(); }