问题
I have a objectchoice field with choices as say YES and NO. I want to handle event when the user selects any one of the choices.
for example when the user selects YES i want a labelField to be added to the screen. and when No is selected this label should be removed from the screen
Please help
回答1:
I think you can do it by implementing FieldChangeListener interface and override on fieldChanged event.
回答2:
In your window class, implement FieldChangeLIstener.
objectChoiceField.setChangeListener(this)
I would recommend doing marking the index of where you want to add/remove the label, so that in fieldChanged event you can more easily do what you want without risk of using an invalid index:
if (selectedIndex == 0) { // Yes
if (!labelField.hasManager()) {
// If the field is not already present, add it to the screen.
insert(labelField, positionToInsertField);
}
} else { // No
if (labelField.hasManager()) {
// Our field is currently on the screen - let's remove it now.
remove(labelField);
}
}
You can find an example of very similar behavior in the code below:
http://svn.bbssh.org/trunk/BBSSH/src/org/bbssh/screens/ConnectionPropertiesScreen.java
Look for function "handleFontTypeChange", invoked from fieldChangeListener. In there, you'll see how based on the current selection (in this case truetype font vs bitmap font) we're adding and remove a control dynamically.
来源:https://stackoverflow.com/questions/3218206/objectchoicefield-in-blackberry