为 SVG 元素设置 OnClickListener

发布于 2024-12-09 04:26:23 字数 92 浏览 0 评论 0原文

假设我有一个 SVG 元素,如下所示。如何添加 onClickListener?

solved, see below.

Say I have an SVG element, as follows. How do I add an onClickListener?

solved, see below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-12-16 04:26:23

我猜你指的是 FieldChangeListener 而不是 OnClickListener (错误的平台;)。 SVGImage 不是 RIM 开发的对象的一部分,因此遗憾的是您无法这样做。任何能够拥有 FieldChangeListner 的东西都必须是 net.rim.device.api.ui.Field 类的子类。

I'm going to guess you're meaning a FieldChangeListener rather than an OnClickListener (wrong platform ;). SVGImage isn't part of the RIM-developed objects, so unfortunately you won't be able to. Anything that is going to be able to have a FieldChangeListner has to be a subclass of the net.rim.device.api.ui.Field class.

温柔戏命师 2024-12-16 04:26:23

以防万一有人对它是如何完成的感兴趣......

try {
            InputStream inputStream = getClass().getResourceAsStream("/svg/sphere1.svg");

            _image = (SVGImage)SVGImage.createImage(inputStream, null);   

            _animator = SVGAnimator.createAnimator(_image, "net.rim.device.api.ui.Field");

            _document = _image.getDocument(); 

            _svg123 = (SVGElement)_document.getElementById("123");


        }
        catch (IOException e) { e.printStackTrace(); }
        Field _svgField = (Field)_animator.getTargetComponent();        

        _svgField.setBackground(blackBackground);

        add(_svgField);

        _svg123.addEventListener("click", this, false);
        _svg123.addEventListener("DOMFocusIn", this, false);
        _svg123.addEventListener("DOMFocusOut", this, false);


    }

    public void handleEvent(Event evt) {

        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "click" ){ Dialog.alert("You clicked 123"); }

        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "DOMFocusIn" ) { ((SVGElement) _document.getElementById("outStroke123")).setTrait("fill", "#FF0000");     }
        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "DOMFocusOut" ) { ((SVGElement) _document.getElementById("outStroke123")).setTrait("fill", "#2F4F75");    }

    }

Just in case someone's interested in how it's done...

try {
            InputStream inputStream = getClass().getResourceAsStream("/svg/sphere1.svg");

            _image = (SVGImage)SVGImage.createImage(inputStream, null);   

            _animator = SVGAnimator.createAnimator(_image, "net.rim.device.api.ui.Field");

            _document = _image.getDocument(); 

            _svg123 = (SVGElement)_document.getElementById("123");


        }
        catch (IOException e) { e.printStackTrace(); }
        Field _svgField = (Field)_animator.getTargetComponent();        

        _svgField.setBackground(blackBackground);

        add(_svgField);

        _svg123.addEventListener("click", this, false);
        _svg123.addEventListener("DOMFocusIn", this, false);
        _svg123.addEventListener("DOMFocusOut", this, false);


    }

    public void handleEvent(Event evt) {

        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "click" ){ Dialog.alert("You clicked 123"); }

        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "DOMFocusIn" ) { ((SVGElement) _document.getElementById("outStroke123")).setTrait("fill", "#FF0000");     }
        if( _svg123 == evt.getCurrentTarget() && evt.getType() == "DOMFocusOut" ) { ((SVGElement) _document.getElementById("outStroke123")).setTrait("fill", "#2F4F75");    }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文