问题
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