問題描述
我正在為大學(xué)做一個項目,讓用戶在地圖上放置一個點,然后為覆蓋對象設(shè)置標(biāo)題和描述.問題是,第二個 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.解決方案很簡單,將所有內(nèi)容放在一個 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
(這是一個基本示例,但它應(yīng)該可以幫助您入門.)
(This is a basic example, but it should get you started.)
您應(yīng)該注意 set
和 add
方法之間的命名差異.setView()
只保存一個View,setMessage()
也一樣.事實上,這對于每個 set
方法都應(yīng)該是正確的,您正在考慮的是 add
命令.add
方法是累積的,它們會構(gòu)建一個您推送的所有內(nèi)容的列表,而 set
方法是單數(shù)的,它們會替換現(xiàn)有數(shù)據(jù).
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.
這篇關(guān)于AlertDialog 中的多個 EditText 對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!