对话框只包含一个根视图,这就是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
(这是一个基本的例子,但它应该让你开始.)
您应该注意set和add方法之间的命名法区别. setView()只保存一个View,对于setMessage()也是如此.事实上,对于每个set方法都应如此,你想到的是添加命令.添加方法是累积的,它们构建了一个列表,列出了所有内容,当set方法是单数时,它们会替换现有数据.