Handle edittexts onKeyListener ?

Posted by hungson175 on Stack Overflow See other posts from Stack Overflow or by hungson175
Published on 2010-12-28T09:58:21Z Indexed on 2010/12/28 10:54 UTC
Read the original article Hit count: 158

Filed under:

Hi everyone,

I am creating login screen with 2 editTexts: etUsername and etPassword.

On the etUsername, user should input the username and press Enter to go to the edit text etPassword, then he inputs the password, press Enter to login. Here are my current codes:

  etUsername.setOnKeyListener(new OnKeyListener() {   
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_ENTER)) {         
     etPassword.requestFocus();
     return true;
    } else
     return false;
   }
  });

  etPassword.setOnKeyListener(new OnKeyListener() {   
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_ENTER)) {         
     loginToServer();
     return true;
    } else
     return false;
   }
  });

But when I input the username, and then press Enter – the program tries to log in to the server.

In the debug mode, I saw that when I pressed Enter once (on the etUsername) then first: etUsername.onKey() is called and then etPassword.onKey() is also called !

Can anyone please tell me, how to implement what I want ?
Thank you very much,
Son

© Stack Overflow or respective owner

Related posts about android