我在GWT中实现了一个复合按钮,并希望实现HasEnabled接口以启用/禁用此按钮上的click事件。这就是我所做的:
public class MyButton extends Composite implements HasText HasClickHandlers, HasEnabled {
FocusPanel panel;
boolean enabled;
public MyButton() {
panel = new FocusPanel();
label = new Label("Test");
panel.add(label);
initWidget(panel);
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return panel.addClickHandler(handler);
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
启用时如何禁用面板上的ClickHandler设置为false?
答案 0 :(得分:0)
HandlerRegistration handlerRegistration = panel.addClickHandler(handler);
如果要禁用它:
if (handlerRegistration != null) {
handlerRegistration.removeHander();
}
答案 1 :(得分:0)
最简单的方法是编写一个包装器单击处理程序,只有在启用该组件时才执行“真正的”单击处理程序:
@Override
public HandlerRegistration addClickHandler(final ClickHandler handler) {
return panel.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if(enabled){
handler.onClick(event);
}
}
});
}