I am using this code:
public boolean onKey(View v, int keyCode, KeyEvent event) { msg = (EditText)findViewById(R.id.msg); String message = msg.getText().toString(); if(keyCode == 66) { //It's hitting here twice. } return false; };
Can anyone please tell me why it's hitting twice when I press enter?
OnKey is fired twice: the first time for key down, and the second time for key up, so you have to filter:
YOUR_VIEW.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { //This is the filter if (event.getAction()!=KeyEvent.ACTION_DOWN) return true; switch (keyCode) { case KeyEvent.KEYCODE_1 : MakeToast(1); break; case KeyEvent.KEYCODE_2 : MakeToast(2); break; case KeyEvent.KEYCODE_3 : MakeToast(3); break; } return true; } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With