我正在创建一个自己的按钮小部件,并希望在此小部件上注册 HasClickHandlers 和 HasMouseoverHandlers。问题是当我使用这个 Widget 并添加 ClickHandler 时,没有反应。
MyButton extends Widget implements HasClickHandlers, HasMouseOverHandlers{
Element elem;
Element child1;
Element child2;
public MyButton(String txt1, String txt2){
elem = Document.get().createDivElement();
child1 = Document.get().createDivElement();
child2 = Document.get().createDivElement();
child1.setInnerText(txt1);
child2.setInnerText(txt2);
elem.appendChild(child1);
elem.appendChild(child2);
setStyleName(elem, "elem");
setStyleName(child1, "child1");
setStyleName(child1, "child2");
setElement(elem);
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return this.addHandler(handler, ClickEvent.getType());
}
@Override
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return this.addDomHandler(handler, MouseOverEvent.getType());
}
}
现在我正在使用这个小部件,应该是一个按钮。
MyButton b1 = new MyButton("Hello", "People");
b1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("Hi");
}
});
这次点击没有反应。
b1.addMouseOverHandler(new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
anotherWidget.getElement().getStyle().setBackgroundColor("yellow");
}
});
mouseOverHandler 有效,但 clickHandler 无效。怎么了?