多个点击处理程序

时间:2014-04-25 07:06:25

标签: gwt button handler

我有不同的3个不同的按钮和不同的onclick事件:

add.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                add();
            }
        });

set.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                set();
            }
        });

get.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event)
            {
                get();
            }
        });

所以现在如果我把这个扩展到10个按钮我的脚本将会很长, 有没有办法通过Methode或分开处理程序?

2 个答案:

答案 0 :(得分:1)

假设您有一些观点:

customview.ui.xml

<g:VerticalPanel>
    <style:Button ui:field="addButton" text="Add"/>
    <style:Button ui:field="setButton" text="Set"/>
    <style:Button ui:field="getButton" text="Get"/>
</g:VerticalPanel>

在View类中定义3个字段和3个处理程序:

CustomView.java

public class CustomView extends ViewWithUiHandlers<CustomUiHandlers>
    implements CustomPresenter.MyView {

    @UiField
    Button addButton;

    @UiField
    Button setButton;

    @UiField
    Button getButton;

    // Here constructor and other code

    @UiHandler("addButton")
    void onAddButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onAddClicked();
    }
}

    @UiHandler("setButton")
    void onSetButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onSetClicked();
    }
}

    @UiHandler("getButton")
    void onGetButtonClicked(ClickEvent event) {
    if (getUiHandlers() != null) {
        getUiHandlers().onGetClicked();
    }
}
}

CustomUiHandlers.java

public interface CustomUiHandlers extends UiHandlers {
    void onAddClicked();
    void onSetClicked();
    void onGetClicked();
}

CustomPresenter.java

public class CustomPresenter extends
    Presenter<CustomPresenter.MyView, CustomPresenter.MyProxy>
            implements CustomUiHandlers {

    // Some code

    @Override
    public void onAddClicked() {
    // Here your code
    }

    @Override
    public void onSetClicked() {
    // Here your code
    }

    @Override
    public void onGetClicked() {
    // Here your code
    }
}

答案 1 :(得分:0)

您可以通过UiBinder将事件处理程序绑定到方法,或者等待GWT的lambda support