Detect Done and Finish keyboard event on Android

0

Detect when the user presses on the key Done

What I intend is once the user has entered the data when pressing Done a validation is done

But if the user decides to stop typing and hide the keyboard by pressing the back button, then also detect that event.

    
asked by Webserveis 20.01.2018 в 19:20
source

1 answer

1

the two events can be detected separately. For the first case, we must put a listener that detects the pressure of buttons and with the validate the button of done to perform an action. This is done as follows

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                //aqui iria tu codigo al presionar el boton enter o done
                return true;
            }
            return false;
        }
    });

For the second question, it occurs to me to listen when the edittext in question loses focus. for that we use the following code

 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
            }
        }
    });

my sources were taken from the following post

link

link

    
answered by 21.01.2018 / 18:53
source