Multiple EditText objects in AlertDialog(AlertDialog 中的多个 EditText 对象)
问题描述
我正在为大学做一个项目,让用户在地图上放置一个点,然后为覆盖对象设置标题和描述.问题是,第二个 EditText
框覆盖了第一个.这是我的对话框代码.
I'm working on a project for college that will let a user place a point on a map and then set the title and description for the overlay object. The problem is, the second EditText
box overwrites the first one. Here is my code for the dialog box.
//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);
任何帮助将不胜感激!谢谢!
Any help would be appreciated!! Thanks!
推荐答案
一个Dialog只包含一个根View,这就是为什么setView()
会覆盖第一个EditText.解决方案很简单,将所有内容放在一个 ViewGroup 中,例如 LinearLayout:
A Dialog only contains one root View, that's why setView()
overwrites the first EditText. The solution is simple put everything in one ViewGroup, for instance a 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
(这是一个基本示例,但它应该可以帮助您入门.)
(This is a basic example, but it should get you started.)
您应该注意 set
和 add
方法之间的命名差异.setView()
只保存一个View,setMessage()
也一样.事实上,这对于每个 set
方法都应该是正确的,您正在考虑的是 add
命令.add
方法是累积的,它们会构建一个您推送的所有内容的列表,而 set
方法是单数的,它们会替换现有数据.
You should take note of the nomenclature difference between a set
and add
method. setView()
only holds one View, the same is similar for setMessage()
. In fact this should be true for every set
method, what you're thinking of are add
commands. add
methods are cumulative, they build a list of everything you push in while set
methods are singular, they replace the existing data.
这篇关于AlertDialog 中的多个 EditText 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:AlertDialog 中的多个 EditText 对象


- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01