您可以构建包含两个 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);