japuentem japuentem - 5 months ago 49
Android Question

How can i know when a edittext lost focus

I need to catch when an EditText loses focus, I've search in users questions but i didn't find any answer.

I use focuschangelistener like this

OnFocusChangeListener foco = new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub

}
};


But it doesn't work for me

Thanks

Answer

Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

 EditText txtEdit = (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
               // code to execute when EditText loses focus
            }
        }
    });