我需要創建一個SuggestBox,它將顯示按下Enter鍵 上的所有選項。 我寫了下面的實現,它似乎是 工作正常。 我希望有人審查我的實施,並讓我知道,如果它 將導致任何特定情況下的問題。 此外,通過調用MultiWordSuggestOracle上的方法 setDefaultSuggestions(),傳遞給此SuggestBox的SuggestOracle應該有 默認建議集。我的 SuggestBox的任何用戶都應該對這個事實透明。因此,我想我會 需要包裝(或擴展)MultiWordSuggestOracle做默認 建議設置。你能推薦一下這樣做的好方法 嗎?SuggestBox GWT顯示輸入鍵上的所有選項
public class SuggestBoxWithAllOptions extends SuggestBox implements
KeyPressHandler {
public SuggestBoxWithAllOptions(MultiWordSuggestOracle oracle) {
super(oracle);
this.addKeyPressHandler(this);
}
@Override
public void onKeyPress(KeyPressEvent event) {
char c = event.getCharCode();
int i = this.getText().length();
if (c == KeyboardListener.KEY_ENTER && i == 0) {
/* Since the query string is null, the default suggestions
will get listed */
this.showSuggestionList();
}
}
}
/* Code for initializing the SuggestBox */
List<String> suggestions = new ArrayList<String>();
suggestions.add("Tablet");
suggestions.add("Capsule");
MultiWordSuggestOracle myOracle = new MultiWordSuggestOracle();
myOracle.addAll(suggestions);
myOracle.setDefaultSuggestionsFromText(suggestions);
SuggestBox mySuggest = new SuggestBoxWithAllOptions(myOracle);