是否可以选择将不同的eventhandler捆绑在一个javafile中?
像:
public interface MyHandlerr extends EventHandler {
void myEvent1(Event1 event);
void myEvent2(Event2 event);
}
在我为每个活动提供一个处理程序的那一刻......但我对此并不满意。
格尔茨
答案 0 :(得分:1)
您可以创建自己的EventHandler
界面来处理多个事件
public interface MultipleEventsHandler extends EventHandler {
void onMyEvent(MyEvent event);
void onMyOtherEvent(MyOtherEvent event);
}
然后在您的事件类中,您可以定义应该调用哪些方法
public class MyEvent extends GwtEvent<MultipleEventsHandler> {
public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();
@Override
public Type<MultipleEventsHandler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(MultipleEventsHandler handler) {
handler.onMyEvent(this);
}
}
public class MyOtherEvent extends GwtEvent<MultipleEventsHandler> {
public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();
@Override
public Type<MultipleEventsHandler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(MultipleEventsHandler handler) {
handler.onMyOtherEvent(this);
}
}
如果您只想减少类/接口的数量,那么您可以将EventHandler
放入事件类中,例如
public class MyEvent extends GwtEvent<MyEvent.Handler> {
public interface Handler extends EventHandler {
void onMyEvent(SomeEvent event);
}
public static final Type<MyEvent.Handler> TYPE = new Type<MyEvent.Handler>();
@Override
public Type<MyEvent.Handler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(MyEvent.Handler handler) {
handler.onMyOtherEvent(this);
}
}