我创建了自己的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
}
});
答案 0 :(得分:1)
我们应该有更多的信息,因为我错过了一些代码,例如,你在哪里将Icon和TextNode附加到DOM?
我做了一个例子,它对我有用。看:
@UiField Label labelText;
public MyWidget(String text) {
super();
//That is the key. Attach the widgets to the dom
setElement(uiBinder.createAndBindUi(this).getElement());
labelText.setText(text);
}
此外,我没有在类中声明我的内部小部件,而是创建了一个UiBinder类。基本上是:
<!DOCTYPE ui:UiBinder SYSTEM 'http://dl.google.com/gwt/DTD/xhtml.ent'>
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<ui:style>
</ui:style>
<g:FlowPanel>
<g:Label ui:field="labelText">
</g:Label>
</g:FlowPanel>
</ui:UiBinder>
在这种情况下,您可以更快速,更轻松地将小部件附加到面板。并且在构造函数中只执行setElement()。
如果您有任何疑问,请询问!