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:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
