4
A
回答
6
問題中的圖片是以下代碼和樣式的屏幕截圖。
public class PeopleBox extends HorizontalPanel implements
ValueChangeHandler<String>, KeyDownHandler {
SuggestBox inputBox;
public PeopleBox() {
inputBox = new SuggestBox(getOracle());
this.setStylePrimaryName("peoplebox");
/*
* Put focus on the SuggestBox, if the user doesn't hit it (The whole
* HorizontalPanel is styled in a way to make it look like a textbox,
* but the actual textbox is smaller than the HorizontalPanel, so you
* need something like this to keep the impression of the whole panel
* being a textbox)
*/
this.addDomHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
inputBox.getTextBox().setFocus(true);
}
}, ClickEvent.getType());
// Display a message in the SuggestBox instead of using a Label to
// display one. The events are used to delete and create this message
inputBox.getTextBox().addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
if (inputBox.getTextBox().getValue().equals(""))
inputBox.getTextBox().setValue("add person...");
}
});
inputBox.getTextBox().addFocusHandler(new FocusHandler() {
public void onFocus(FocusEvent event) {
if (inputBox.getTextBox().getValue().equals("add person..."))
inputBox.getTextBox().setValue("");
}
});
inputBox.addValueChangeHandler(this);
inputBox.getTextBox().addKeyDownHandler(this);
inputBox.setStylePrimaryName("peoplebox-input");
inputBox.getTextBox().setValue("add person...");
this.add(inputBox);
}
//displays the selected person
public void onValueChange(ValueChangeEvent<String> event) {
this.insert(new PeopleDisplay(event.getValue()),
this.getWidgetCount() - 1);
this.inputBox.setValue("");
}
//deletes the person on the left side of the SuggestBox, if you hit backspace
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE
&& this.inputBox.getValue().equals("")
&& this.getWidgetCount() > 1) {
this.remove(this.getWidgetCount() - 2);
}
}
public MultiWordSuggestOracle getOracle() {
MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
oracle.add("Mark Zuckerberg");
oracle.add("Tyler Winklevoss");
oracle.add("Cameron Winklevoss");
return oracle;
}
private class PeopleDisplay extends Grid implements ClickHandler {
public PeopleDisplay(String name) {
super(1, 2);
this.setStylePrimaryName("peoplebox-peopledisplay");
this.addClickHandler(this);
this.setText(0, 0, name);
this.setText(0, 1, "X");
}
public void onClick(ClickEvent event) {
if (this.getCellForEvent(event).getCellIndex() == 1)
this.setVisible(false);
}
}
}
的CSS(我不使用GWT主題和coulnd't找到一種方法,風格人的名單,所以我只是用相同名稱的GWT的默認操作):
.peoplebox {
background:white;
height:37px;
padding:0px 4px 0px 4px;
border: 1px solid #060b15;
border-top-left-radius: 5px 5px;
border-top-right-radius: 5px 5px;
border-bottom-left-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
margin-right:10px;
cursor:text;
}
.peoplebox-input {
border:0px;
outline:none;
padding:6px;
font-size:inherit;
}
.peoplebox-peopledisplay {
background:#060b15;
color:#f1f1f1;
border-top-left-radius: 5px 5px;
border-top-right-radius: 5px 5px;
border-bottom-left-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
padding:0xp 2px 0px 2px;
height:16px;
margin-right:5px;
margin-top:5px;
}
.gwt-SuggestBoxPopup {
background:white;
border-top-left-radius: 5px 5px;
border-top-right-radius: 5px 5px;
border-bottom-left-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
border: 1px solid #060b15;
}
.gwt-SuggestBoxPopup .item {
}
.gwt-SuggestBoxPopup .item-selected {
background:#060b15;
color:#f1f1f1;
}
相關問題
- 1. 如何構建GWT加載對話框?
- 2. 如何在gwt中構建一個flowcover?
- 3. 如何在輸入框中
- 4. 在Eclipse中構建GWT UI
- 5. 如何記住GWT中的文本框輸入歷史記錄
- 6. 在GWT中構建一個樹構件
- 7. 如何在win32窗口中創建一個嵌入式文本輸入框
- 8. 如何在GWT表格樣式構建器中添加空格:nowrap樣式
- 9. 如何在輸入html格式輸入框時獲取值?
- 10. GWT:如何確保在構建GWT頁面後運行javascript
- 11. 如何構建拉丁文輸入法
- 12. 如何在Ionic中居中輸入框
- 13. 如何使用輸入數組構建角2反應形式
- 14. 如何構建一個REGEX模式來驗證輸入字段
- 15. 如何在iOS中構建計算器樣式的數字輸入字段?
- 16. 如何在JavaScript中構建PDF框架?
- 17. 如何在Xcode中構建框架?
- 18. GWT MVP - 如何構建應用程序
- 19. 不知道如何創建和錨一個div在輸入框中輸入時
- 20. 如何對齊輸入框的形式
- 21. 如何將輸入框放在aframe中?
- 22. GWT在Eclipse中發佈構建命令
- 23. 使用uiBinder在gwt中構建表格
- 24. 如何在Corona(Lua)中創建輸入框?
- 25. 如何在javascript中創建輸入文本框
- 26. 如何在BlueJ「創建對象」對話框中輸入LocalDate值
- 27. 如何在WinRT中使用XAML創建100%輸入對話框
- 28. 如何在Android中創建文本輸入對話框?
- 29. 無法在花式框中輸入CKeditor
- 30. 如何檢查在文本框中輸入的時間格式
謝謝你的一個很好的例子!但是你應該使用onSelection()而不是onValueChange()。這對我們在這裏要做的事情更合適。 – 2013-04-03 19:05:58