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 对象
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01