开发者

Blackberry - ButtonField visibility depending on LabelField focus

开发者 https://www.devze.com 2022-12-16 21:10 出处:网络
I have 3 fields in my ui 2 buttons开发者_StackOverflow, one on top and other on buttom of a label field...when ever the label field gets focus i want the buttons to appear and i should be able to clic

I have 3 fields in my ui 2 buttons开发者_StackOverflow, one on top and other on buttom of a label field...when ever the label field gets focus i want the buttons to appear and i should be able to click on them...and when the label field loses focus the buttons should disappears..how can i do this...


put some manager field as a placeholder at button position, then add a FocusChangeListener to label and use add/delete field on focusChanged to show/hide button.

UPDATE
Since every focus change from fields may change layout, think its better to add listener to every field added to screen and placeholder:

class Scr extends MainScreen {
    HorizontalFieldManager placeholder = new HorizontalFieldManager() {
        public void add(Field field) {
            if (field.getFocusListener() != null)
                field.setFocusListener(null);
            field.setFocusListener(focusListener);
            super.add(field);
        }
    };
    ButtonField buttonField = new ButtonField("button",
            ButtonField.CONSUME_CLICK);
    LabelField labelField = new LabelField("label", FOCUSABLE);

    public Scr() {
        add(placeholder);
        add(labelField);
        add(new LabelField("label2", FOCUSABLE));
    }

    public void add(Field field) {
        if (!(field instanceof Manager)) {
            if (field.getFocusListener() != null)
                field.setFocusListener(null);
            field.setFocusListener(focusListener);
        }
        super.add(field);
    }

    FocusChangeListener focusListener = new FocusChangeListener() {
        public void focusChanged(Field field, int eventType) {
            if (eventType == FOCUS_GAINED) {
                if (field == labelField) {
                    if (buttonField.getManager() == null)
                        placeholder.add(buttonField);
                } else if (field != buttonField)
                    placeholder.delete(buttonField);
            }
        }
    };
}
0

精彩评论

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

关注公众号