super.onKeyDown (keyCode, event)丢失了,我想你应该试试这个:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
if (KeyCode == KeyEvent.KEYCODE.KEYCODE_APP_SWITCH && event.getRepeatCount() == 0)
{
Log.d ("onkeydown","app switch key");
}
else if (KeyCode == KeyEvent.KEYCODE.KEYCODE_HOME && event.getRepeatCount() == 0)
{
Log.d ("onkeydown","home key");
}
return true;
}
使用 switch 语句做同样的事情:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
Toast.makeText(ListViewActivity.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_1:
Toast.makeText(ListViewActivity.this, "Pressed 1", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_HOME:
Toast.makeText(ListViewActivity.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_BACK:
Toast.makeText(ListViewActivity.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
finish();
return true;
}
return false;
}