I have a <div id="test"><input type="button" value="OK" /></div>
html tag.
I used:
((HasClickHandlers)RootPanel.get("test").getWidget(0)).addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert('sss');
}
}
I executed but no action.
Update:
package com.example.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.RootPanel;
public class ExampleWebApp implements EntryPoint {
public void onModuleLoad() {
((HasClickHandlers) RootPanel.get("test").getWidget(0)).addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("i got it");
}
});
}
}
HTML:
<table>
<tr>
<div id="test">
<input type=butto开发者_StackOverflown onClick="" value='click here'>
</div>
</tr>
</table>
The GWT Button widget is a button
tag and not a input
tag. Which means you can't use the GWT Button widget in this case. To make it work you need to create your own widget, which can be based on the widget ButtonBase
, but needs to be initialized with an InputElement
object instead of a ButtonElement
.
The next step to get tag from html is to add something similar to the static wrap
method present in most widgets. Here is how it would be used in your example when the input
would have been a button
tag:
Button.wrap(RootPanel.get("test").getWidget(0).getElement()).addClickHandler(
new ClickHandler() {
@Override public void onClick(ClickEvent event) {
Window.alert('sss');
}
});
In you case you could add a wrap
method to your custom input widget. See the Button
widget implementation of te wrap
method, it's the same, expect of course for the creation of the widget itself.
You can't just take an html button and try to add click handlers to it. You need to create the button using gwt code. Try:
<div id="test"></div>
And then:
Button button = new Button("OK");
RootPanel.get("test").add(button);
button.addClickHandler(new ClickHandler() {...});
精彩评论