BlackBerry - 如何在自定义Manager中正确处理TAP / CLICK

时间:2013-10-15 20:39:25

标签: java blackberry

我需要为自定义管理器设置点击/点击事件。

管理器包含几个元素,但我需要整体 管理员会响应该事件,因此如果用户点击/单击任何元素 在Manager内部,也应该调用事件处理程序。 我所做的是将一个changeListener附加到一个占用所有宽度和高度的NullField,但是从不调用fieldChanged方法

public class TestManager extends Manager{
    private BitmapField _icon;
    private LabelField _lblTitle;
    private NullField nullField;
    public TestManager(Bitmap pIcon){
        super(Manager.FOCUSABLE);
        _icon = new BitmapField(pIcon);
        _lblTitle = new LabelField("This is the title");
        nullField = new NullField(NullField.FOCUSABLE);
        add(_icon);
        add(_lblTitle);
        add(nullField);
            nullField.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            doSomething();
        }
    });

    }

    public void doSomething(){
    System.out.println("do something!!");
}
    public void paint(Graphics graphics)
    {
       graphics.setColor(Color.DELIMITER_COLOR);
       graphics.fillRect(0, 20, Display.getWidth(), 50);

       super.paint(graphics);
    }

    protected void paintBackground(Graphics arg0) {
       arg0.setColor(Color.LIGHTGRAY);
       arg0.fillRect(0, 0, Display.getWidth(), Display.getHeight());
       super.paint(arg0);
    }

    protected void sublayout(int width, int height) {
        setPositionChild(_icon, UIFactory.getBestX(10), UIFactory.getBestX(8));
        setPositionChild(_lblTitle, UIFactory.getBestX(60), _topMarginTitle!=0?UIFactory.getBestX(_topMarginTitle):UIFactory.getBestX(15));
        setPositionChild(nullField, 0, 0);
        layoutChild(_icon, width, UIFactory.getBestX(50));
        layoutChild(_lblTitle, width, UIFactory.getBestX(50));
        layoutChild(nullField, width, height);
        setExtent(width, height);
    }

      protected boolean navigationClick(int status, int time){ 
            invalidate();
            doSomething();
            return super.navigationClick(status, time);
        }

}

如何让整个经理回复点击/点按?

我正在使用API​​ 5.0

提前致谢!

1 个答案:

答案 0 :(得分:1)

BB论坛也提出了问题,我在那里做出回应: BlackBerry-How-to-properly-handle-a-TAP-CLICK-in-a-custom

此外,这是解决问题的方法:

public class BaseTouchManager extends Manager {

    private FieldChangeListener _callBack;

    public BaseTouchManager(FieldChangeListener pCallBack) {
        super(0);
        _callBack = pCallBack;
        setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                _callBack.fieldChanged(field, context);
            }
        });

        add(new NullField(Field.FOCUSABLE));
    }

    protected void sublayout(int width, int height) {
        setExtent(Display.getHeight()/2, 100);
    }

    protected boolean navigationClick(int status, int time) {
            fieldChangeNotify(1);
            return true;
    }

    public void paint(Graphics graphics) {
        if(_callBack!=null){
            if(isFocus())
                graphics.setColor(Color.LIGHTBLUE);
            else
                graphics.setColor(Color.WHITE);
            graphics.fillRect(this.getWidth() - 10, 
                    this.getHeight() - 10, 
                    10, 10);

        }
        super.paint(graphics);  
    }
}

原始海报实际上提出了不同的解决方案,请查看BB论坛帖子了解更多信息。