开发者

Set an OnClickListener for an SVG element

开发者 https://www.devze.com 2023-04-12 16:49 出处:网络
Say I have an S开发者_运维技巧VG element, as follows.How do I add an onClickListener? solved, see below.

Say I have an S开发者_运维技巧VG element, as follows. How do I add an onClickListener?

solved, see below.


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.


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");    }

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号