处理html按钮代号为1的事件

时间:2015-02-09 17:59:57

标签: codenameone

在表单中我嵌入了WebBrowser组件,其中包含许多用于丰富ui的html内容(主要是表格,按钮)。点击html按钮可以处理事件吗?

1 个答案:

答案 0 :(得分:0)

当然,请查看厨房水槽演示样本。

通常只需导航到活动的网址并实施您自己的BrowserNavigationCallback即可处理该特定网址的导航。

这是来自Kitchen Sink演示的代码注意setBrowserNavigationCallback块:

    final WebBrowser wb = new WebBrowser();
    if(wb.getInternal() instanceof BrowserComponent) {
        Button btn = new Button("Add");
        final TextArea content = new TextArea();
        Container north = new Container(new BorderLayout());
        north.addComponent(BorderLayout.CENTER, content);
        north.addComponent(BorderLayout.EAST, btn);
        cnt.addComponent(BorderLayout.NORTH, north);
        content.setHint("Add to web document");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ((BrowserComponent)wb.getInternal()).execute("fnc('" + content.getText() + "');");
            }
        });
        ((BrowserComponent)wb.getInternal()).setBrowserNavigationCallback(new BrowserNavigationCallback() {
            public boolean shouldNavigate(String url) {
                if(url.startsWith("http://sayhello")) {
                    // warning!!! This is not on the EDT and this method MUST return immediately!
                    Display.getInstance().callSerially(new Runnable() {
                        public void run() {
                            ((BrowserComponent)wb.getInternal()).execute("fnc('this was written by Java code!')");
                        }
                    });
                    return false;
                }
                return true;
            }
        });
    }