Kei*_*ith 12 java events gwt event-handling
我想在第一次收到事件时删除GWT事件处理程序.我还想避免使用非必要的跟踪注册对象来污染我的类.我目前将其编码为:
final HandlerRegistration[] registrationRef = new HandlerRegistration[1];
registrationRef[0] = dialog.addFooHandler(new FooHandler()
{
public void onFoo(FooEvent event)
{
HandlerRegistration removeMe = registrationRef[0];
if(removeMe != null)
{
removeMe.removeHandler();
}
// do stuff here
}
});
但使用registrationRef会降低代码的可读性.有没有更好的方法来做到这一点而不向我的班级添加变量?
Igo*_*mer 12
我只是让HandlerRegistration对象成为封闭类的一个字段,这样你就不会受到编译器的困扰,而且它比洗涤数组和东西更"优雅":
public class TestWidget extends Composite {
//...
HandlerRegistration handler;
public TestWidget() {
// ...
handler = button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// ...
handler.removeHandler();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)