How to handle ButtonField & BitmapField Click (Touch) events in Blackberry Storm?

佐手、 提交于 2019-12-03 16:43:58

First of all, don't forget to add hfm to screen ;)
Actually button click works fine.
Now, to make bitmap click works as well, implement protected boolean touchEvent(TouchEvent message) for your BitmapField. It will be better to create extended class:

class MyCanvas extends MainScreen implements FieldChangeListener {
    HorizontalFieldManager hfm;
    private Bitmap startBitmap;
    private BitmapField startBitmapField;
    private ButtonField okButton;
    private ButtonField cancelButton;

    MyCanvas() {
        hfm = new HorizontalFieldManager();
        add(hfm);

        startBitmap = Bitmap.getBitmapResource("start.png");
        startBitmapField = new TouchBitmapField(startBitmap);
        startBitmapField.setChangeListener(this);
        hfm.add(startBitmapField);

        okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK
                | ButtonField.NEVER_DIRTY);
        okButton.setChangeListener(this);
        hfm.add(okButton);

        cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK
                | ButtonField.NEVER_DIRTY);
        cancelButton.setChangeListener(this);
        hfm.add(cancelButton);
    }

    public void fieldChanged(Field field, int context) {
        if (field == startBitmapField) {
            System.out.println("Touched START...");
        } else if (field == okButton) {
            System.out.println("Touched Ok...");
        } else if (field == cancelButton) {
            System.out.println("Touched Cancel...");
        }
    }
}

class TouchBitmapField extends BitmapField {
    public TouchBitmapField(Bitmap startBitmap) {
        super(startBitmap);
    }

    protected boolean touchEvent(TouchEvent message) {
        if (TouchEvent.CLICK == message.getEvent()) {
            FieldChangeListener listener = getChangeListener();
            if (null != listener)
                listener.fieldChanged(this, 1);
        }
        return super.touchEvent(message);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!