Automatically change focus among Editfields in Blackberry
-
27-06-2021 - |
Question
I have five EditField
objects in my BlackBerry app, each one will accept only one numeric character.
I want to change the focus from the first EditField
to the second EditField
when a character is entered. Note the focus from one to another EditField
must go automatically and not by pressing Enter key or some other key.
Solution
You want to set a FieldChangeListener
on the EditField
to monitor when the contents of the field changes. Once the user has entered a single character you can move to the next field by calling Field.setFocus()
.
OTHER TIPS
Lets assume your EditField
s are added to screen one by one.
You could use next code:
editField<i>.setFieldChangeListener(this);
...
public void fieldChanged(Field field, int status) {
if (field instanceof EditField) {
EditField editField = (EditField)field;
if (field.getText().length() > 0) {//don't move focus in case of deleted text
Manager manager = field.getManager();
Field nextField = manager.getField(manager.getFieldIndex(editField) + 1);
if (nextField instanceof EditField) {
nextField.setFocus();
}
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow