[英]GWT: Drag&Drop Event for TextField?
I wonder how it is possible to listen for the drop event for a TextField
. 我想知道如何监听
TextField
的drop事件。 I need to intercept that events in order to do value checks. 我需要拦截该事件以便进行值检查。
I wrote an extended TextBox
, which fires TextChangeEvents
. 我写了一个扩展的
TextBox
,它触发了TextChangeEvents
。 But when i drop a text into the text field, I don't get a call for the onBrowserEvent(Event event)
method. 但是,当我将文本放入文本字段时,我没有调用
onBrowserEvent(Event event)
方法。
Any hint? 有什么提示吗?
public class ExtendedTextBox extends TextBox {
/**
* Creates an empty extended text box.
*/
public ExtendedTextBox() {
super();
// Catch the browser events.
sinkEvents(Event.ONKEYUP);
sinkEvents(Event.ONPASTE);
}
@Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
System.out.println(event.getTypeInt());
switch (event.getTypeInt()) {
case Event.ONKEYUP:
case Event.ONPASTE: {
// Fire the event after the text box shows the new data.
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
fireEvent(new TextChangeEvent(ExtendedTextBox.this.getText()));
}
});
break;
}
default:
}
}
/**
* Add a given {@link TextChangeEventHandler} to the widget.
*
* @param handler
* the handler
* @return {@link HandlerRegistration} used to remove the handler
*/
public HandlerRegistration addTextChangeEventHandler(TextChangeEventHandler handler) {
return addHandler(handler, TextChangeEvent.TYPE);
}
}
A TextBox should fire a ChangeEvent
(or ValueChangeEvent
- don't know at the moment). TextBox应该触发一个
ChangeEvent
(或ValueChangeEvent
目前尚不知道)。 So you could easily do something like this inside your constructor: 因此,您可以轻松地在构造函数中执行以下操作:
this.addChangeHandler(new ChangeHandler ...
If that's not enough, you could also listen to the Focus
event. 如果这还不够,您还可以收听
Focus
事件。
Thanks for your help. 谢谢你的帮助。 I figured it out by myself today and it is quiet simple.
我今天自己弄清楚了,它很简单。 I didn't notice, that there is already the
DropHandler
. 我没有注意到,已经有了
DropHandler
。
You can do this for example in the constructor: 例如,您可以在构造函数中执行此操作:
addDropHandler(new DropHandler() {
@Override
public void onDrop(DropEvent event) {
if(!event.getDataTransfer().getData("text/plain").isEmpty()) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
fireEvent(new TextChangeEvent(ExtendedTextBox.this.getText()));
}
});
}
}
});
To prevent the default behaviour of the TextField
just add event.preventDefault();
为了防止
TextField
的默认行为,只需添加event.preventDefault();
. 。 Now you can get the input by calling
event.getDataTransfer().getData("text/plain")
and do your input validation etc. 现在,您可以通过调用
event.getDataTransfer().getData("text/plain")
进行输入,并进行输入验证等。
And as side effect the onBrowserEvent(Event event)
method is called now by dropping something into the text field. 作为副作用,现在通过将某些内容放入文本字段来调用
onBrowserEvent(Event event)
方法。 But the type int is -1
, so it's not really helpful at this place. 但是int类型是
-1
,所以在这个地方并没有太大帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.