我需要创建一个SuggestBox,在按下时将显示所有选项
Enter键。
我已经写了以下实现,看来是
工作正常。
我希望有人审查我的实现情况,并让我知道
在任何特定情况下都会引起问题。
另外,要传递给此SuggestBox的SuggestOracle应该具有
通过调用方法设置默认建议
MultiWordSuggestOracle上的setDefaultSuggestions()。我的任何用户
对这个事实,SuggestBox应该是透明的。因此,我想我会
需要包装(或扩展)MultiWordSuggestOracle以执行默认设置
建议设置。您能推荐一个好方法吗
这样做吗?
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);
最佳答案
对我来说,这看起来不错。另一种方法是添加一个按钮以显示所有建议。可以将按钮设置为看起来像下拉框箭头的样式。
public class DropDownSuggestBox extends Composite {
public DropDownSuggestBox(final SuggestBox suggestBox) {
FlowPanel layout = new FlowPanel();
final Button dropDownButton = new Button();
dropDownButton.setStyleName("slate-DropdownIcon");
dropDownButton.setText("Show Options");
dropDownButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
suggestBox.setFocus(true);
suggestBox.showSuggestionList();
}
});
layout.add(suggestBox);
layout.add(dropDownButton);
initWidget(layout);
setStylePrimaryName("slate-DropDownSuggestBox");
}
}
关于gwt - 在Enter键上显示所有选项的SuggestBox GWT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3039436/