📜  GWT建议框

📅  最后修改于: 2021-01-02 12:42:58             🧑  作者: Mango

GWT建议框

GWT建议框是一个文本框或文本区域,显示与用户输入匹配的一组预配置选择。每个SuggestBox与一个SuggestOracle相关联。在给定特定查询字符串的情况下,SuggestOracle用于提供一组选择。

GWT建议框语法

public class SuggestBox extends Composite

GWT建议框嵌套类

Class Description
SuggestBox.DefaultSuggestionDisplay It is the default implementation of SuggestBox.SuggestionDisplay displays suggestions in a PopupPanel beneath the SuggestBox.
SuggestBox.SuggestionCallback It is the callback used when a user selects a SuggestOracle.Suggestion.
It is the callback used when a user selects a SuggestOracle.Suggestion. It is used to display suggestions to the user.

GWT建议框构造函数

Constructor Description
SuggestBox() It is a default constructor for SuggestBox.
SuggestBox(SuggestOracle oracle) It is a constructor for SuggestBox.
SuggestBox(SuggestOracle oracle, ValueBoxBase box) It extends suggest oracle and value box.
SuggestBox(SuggestOracle oracle, ValueBoxBase box, SuggestBox.SuggestionDisplay suggestDisplay) It display SuggestOracle and ValueBox in a single SuggestBox.

GWT建议框常用方法

Modifier and types Methods Description
void addChangeListener(ChangeListener listener) It gets a text box.
void addClickListener(ClickListener listener) It gets a input on click.
void addEventHandler(SuggestionHandler handler) It adds a event handler.
void addFocusListener(FocusListener listener) It can add blur or focus to particular area.
void addKeyboardListener(KeyboardListener listener) It takes input through keyboard.
HandlerRegistration addKeyDownHandler(KeyDownHandler handler) It adds a KeyDownEvent handler.
HandlerRegistration addKeyPressHandler(KeyPressHandler handler) It adds a KeyPressEvent handler.
HandlerRegistration addKeyUpHandler(KeyUpHandler handler) It adds a KeyUpEvent handler.
HandlerRegistration addSelectionHandler(SelectionHandler handler) It adds a SelectionEvent handler.
HandlerRegistration addValueChangeHandler(ValueChangeHandler handler) It adds a ValueChangeEvent handler.
boolean isAnimationEnabled() It displays animation.
boolean isAutoSelectEnabled() It returns whether or not the first suggestion will be automatically selected.
boolean isEnabled() It gets whether this widget is enabled.
boolean isSuggestionListShowing() It checks if the SuggestBox.SuggestionDisplay is showing.
protected void onEnsureDebugId(java.lang.String baseID) It is called when the user sets the id using the UIObject.ensureDebugId(String) method.

GWT建议框示例

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MyEntryPoint implements EntryPoint {

    public static final String TOYOTA = "Toyota";
    public static final String SEAT = "Seat";
    public static final String SUBARU = "Subaru";
    public static final String AUDI = "Audi";
    public static final String ASTON_MARTIN = "Aston Martin";
   
    @Override
    public void onModuleLoad() {
        SuggestBox carsSuggestBox = new SuggestBox(getCarsOracle());
       
        HorizontalPanel carsPanel = new HorizontalPanel();
        carsPanel.add(new Label("Select a brand:"));
        carsPanel.add(carsSuggestBox);
             
        VerticalPanel formPanel = new VerticalPanel();
        formPanel.add(carsPanel);
       
        RootPanel.get("container").add(formPanel);
    }
   
    private MultiWordSuggestOracle getCarsOracle(){
        MultiWordSuggestOracle carsOracle = new MultiWordSuggestOracle();
        carsOracle.add(TOYOTA);
        carsOracle.add(SEAT);
        carsOracle.add(ASTON_MARTIN);
        carsOracle.add(AUDI);
        carsOracle.add(SUBARU);
       
        return carsOracle;
    }
   
}

输出: