2

有没有办法在 GWT 中使用 Clickhandler 接口来构建自定义子类?我需要 ClickHandlers,它应该对每一次点击做出反应。对于已实现的 ClickHandler 而言,实现此行为不是问题,但如果不必将此代码片段添加到每个 ListBox 元素,那就太好了:

 boolean ingoreClick;
 lastSelection =  -1 ;
 listBox.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {

        if (!ignoreClick) {
            lastSelection = listBox.getSelectedIndex();
        }
        ignoreClick = !ignoreClick;     
    }
});

现在想象一下有listbox1、listbox2等等。

4

2 回答 2

1

是的,因为Clickhandleris 和 interface 您可以在自定义类中实现任何行为并在任何地方使用它。

  public class MyClickHancler implements ClickHandler {
    boolean first = false;
    String name;

    public MyClickHancler(String name) {
      this.name = name;
    }

    public void onClick(ClickEvent event) {
      if (first = !first) {
        System.out.println("First click on " + name);
      } else {
        System.out.println("Second click on " + name);
      }
    }
  }

  [...]

  widget1.addClickHandler(new MyClickHancler("widget-1"));
  widget2.addClickHandler(new MyClickHancler("widget-2"));
于 2013-04-05T08:34:59.617 回答
1

嗯,很简单,Manolo的回答基本解决了,你只需要稍微改进一下即可:

public abstract class EvenClickHandler implements ClickHandler {
    private boolean eventClick= true;

    public void onClick(ClickEvent event) {
        if (eventClick = !eventClick) {
          onEvenClick(event);
        }
    }


    protected abstract void onEvenClick(ClickEvent e);

}

...

listBox.addClickHandler(new EvenClickHandler (){

   protected void onEvenClick(ClickEvent e){
      lastSelection = listBox.getSelectedIndex();
   }

});
于 2013-04-05T11:41:45.043 回答