How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?

SSS picture SSS · Aug 16, 2012 · Viewed 22.2k times · Source

I have 7 edittext boxes in my xml. I'm using the OnFocusChangeListener to read the value from edittext and i'm using that value for my calculation.I want to make my edittext to lose its focus when i click on the done button in the soft keyboard.so that i can get the value in the edittext.

Answer

ρяσѕρєя K picture ρяσѕρєя K · Aug 16, 2012

Call clearFocus method of EditText to lose focus when done button is clicked from soft-keyboard. do it as:

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //Clear focus here from edittext
             editText.clearFocus();
        }
    return false;
    }
});

and also add android:imeOptions="actionDone" in edittext xml