SuggestBox GWT显示Enter键上的所有选项

Vis*_*ngh 5 gwt

我需要创建一个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); 
Run Code Online (Sandbox Code Playgroud)

小智 5

这看起来对我很好.另一种方法是添加一个按钮来显示所有建议.按钮的样式可以看起来像下拉框箭头.

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");
}

}
Run Code Online (Sandbox Code Playgroud)