AlertDialog 中的多个 EditText 对象 [英] Multiple EditText objects in AlertDialog

查看:21
本文介绍了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.)

您应该注意 setadd 方法之间的命名差异.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 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆