android - AlertDialog 中的多个 EditText 对象

标签 android google-maps dialog android-edittext

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

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

最佳答案

一个对话框只包含一个 Root View ,这就是为什么 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() 只保存一个View,setMessage() 也一样。事实上,这对于每个 set 方法都应该是正确的,您正在考虑的是 add 命令。 add 方法是累积的,它们会构建一个您推送的所有内容的列表,而 set 方法是单数的,它们会替换现有数据。

关于android - AlertDialog 中的多个 EditText 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876624/

相关文章:

javascript - 重新定位谷歌地图的html链接

c# - 从 SQL 数据库加载纬度和经度到谷歌地图

java - 使用 org.apache.commons.io.FileUtils 时 Android 内存泄漏

android - xmlns :tools namespace attribute not reflecting in plugin Element

javascript - 谷歌地图JavaScript代码的混淆

java - JOptionPane.ShowConfirmDialog 多个输入,焦点文本字段

Wordpress 插件管理页面中的 jquery 对话框,按钮都出现在右上角重叠,而不是在下方的按钮 Pane 中

php - jQuery 就绪函数在对话框中被调用两次

java - 无法使用 Retrofit 获得 json 响应

android - 从 AsyncTask 返回 JSON 数组