0

Basically, I am validating a field when the the field loses focus. When an error occurs when we check at the OnFocusChangeListener, I want it to cancel the focus and stay on the same field. Is there a way to cancel the focus on it?

public void onFocusChange(View v, boolean hasFocus) 
{
        if (!hasFocus)
        {
            if(checkError(v))
            {
               // Cancel focus here
            }
        } 
}
Frank
  • 3,073
  • 5
  • 40
  • 67

1 Answers1

0

I think requestFocus() should do the trick, see this answer for usage: https://stackoverflow.com/a/8077842/1173391


Update:

I think you are missing the check for the view id, because the onFocusChange method is implemented via interface in your class:

try setting this:

if (v.getId() == myEditText.id)

as first check on the focus change method; you have to do it for every edit-field (or use a switch instead of if) ....

OR

attach an OnFocusChangeListener to every single field:

edittext1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }
});
Community
  • 1
  • 1
Nickolaus
  • 4,785
  • 4
  • 38
  • 60