views:

364

answers:

1

I am having three EditField i have a set focuslistener for the second editfield alone. when the focus is lost i am checking whether the field is empty or not if the field is empty it will show the popup message .Now the problem is once the user came to that field he could not move to other fields without entering any fields but i want user can move to the field which is above to that field and he could not to move to the below field.How to handle this situation? Please share ur ideas.

+1  A: 

I suppose it's for validation functionality? See proposed validation strategy: we have interface IValidated which can be checked for proper data:

interface IValidated {
 public boolean validate();
}

Now, any field, like list or checkbox or choice can implement it.
Next, there is onFocus event in each field, there you can validate previouse IValidated field and setFocus back if it's fail.

See EditField example:

class ValidatedEdit extends BasicEditField implements IValidated {
 private static final String EMPTY_STRING = "";

 public ValidatedEdit(String label, String value) {
  super(label, value);
 }

 protected void onFocus(int direction) {
  // it's from upper field
  if (direction > 0 && getIndex() > 0) {
   // get upper field
   Field field = getManager().getField(getIndex() - 1);
   // if its IValidated
   if (field instanceof IValidated) {
    IValidated validated = (IValidated) field;
    // validate, if false set focus to IValidated
    if (!validated.validate()) {
     field.setFocus();
     return;
    }
   }
  }
  super.onFocus(direction);
 }

 public boolean validate() {
  return !getText().equalsIgnoreCase(EMPTY_STRING);
 }
}

Example of use:

class Scr extends MainScreen {
 ValidatedEdit mEditField1 = new ValidatedEdit("field#1", "");
 ValidatedEdit mEditField2 = new ValidatedEdit("field#2", "");
 ValidatedEdit mEditField3 = new ValidatedEdit("field#3", "");

 public Scr() {
  add(mEditField1);
  add(mEditField2);
  add(mEditField3);
 }
}
Max Gontar