Blackberry - ButtonField visibility depending on LabelField focus

混江龙づ霸主 提交于 2019-12-24 16:05:49

问题


I have 3 fields in my ui 2 buttons, 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...


回答1:


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


来源:https://stackoverflow.com/questions/2061479/blackberry-buttonfield-visibility-depending-on-labelfield-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!