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;
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…