为什么onKey()调用两次?

我正在使用这个代码:

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

任何人都可以告诉我,为什么当我按下回车键时,会碰到两次?

OnKey被激发两次:第一次按下键,第二次键入,所以你必须过滤:

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