我创建了自己的 gwt 小部件,其中包含一个 TextLabel 和一个图像。我需要这个小部件是可聚焦的。我从界面 hasFocusHandler、focusable 和 blurhandler 扩展了这个小部件。没有任何工作。当我单击小部件或在小部件外部时,我既没有收到焦点事件也没有收到模糊事件。我应该怎么办?
我的代码:
public class MyWidget extends ComplexPanel implements HasFocusHandlers, HasBlurHandlers, HasClickHandlers, Focusable{
private Icon icon = new Icon();
private TextNode text = new TextNode("");
private static final FocusImpl impl = FocusImpl.getFocusImplForWidget();
protected static FocusImpl getFocusImpl() {
return impl;
}
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
@Override
public HandlerRegistration addBlurHandler(BlurHandler handler) {
return addDomHandler(handler,BlurEvent.getType());
}
@Override
public HandlerRegistration addFocusHandler(FocusHandler handler) {
return addDomHandler(handler, FocusEvent.getType());
}
@Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONCLICK:
if (isEnabled()) {
super.onBrowserEvent(event);
}
break;
default:
super.onBrowserEvent(event);
break;
}
}
public int getTabIndex() {
return impl.getTabIndex(getElement());
}
@Override
public void setAccessKey(char key) {
DOM.setElementProperty(getElement(), "accessKey", "" + key);
}
@Override
public void setFocus(boolean focused) {
if (focused) {
impl.focus(getElement());
} else {
impl.blur(getElement());
}
}
@Override
public void setTabIndex(int index) {
impl.setTabIndex(getElement(), index);
}
@Override
protected void onAttach() {
super.onAttach();
int tabIndex = getTabIndex();
if (-1 == tabIndex) {
setTabIndex(0);
}
}
}
我的实现不起作用(当我单击 myWidget 时,我没有收到 onFocus 调用)
mywidget.addFocusHandler(new FocusHandler()
{
@Override
public void onFocus(FocusEvent event) {
//DO SOMETHING
}
});