下面的是官方的bolg 觉得不错就引用了http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
-
- return true;
- }
-
- return super.onKeyDown(keyCode, event);
- }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
- @Override
- public void onBackPressed() {
-
- return;
- }
@Override
public void onBackPressed() {
// do something on back.
return;
}
长按事件
- @Override
- public boolean onKeyLongPress(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_CALL) {
-
-
-
-
-
- return true;
- }
- return super.onKeyLongPress(keyCode, event);
- }
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL) {
// a long press of the call key.
// do our work, returning true to consume it. by
// returning true, the framework knows an action has
// been performed on the long press, so will set the
// canceled flag for the following up event.
return true;
}
return super.onKeyLongPress(keyCode, event);
}
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_CALL && event.isTracking()
- && !event.isCanceled()) {
-
-
-
- return true;
- }
- return super.onKeyUp(keyCode, event);
- }
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL && event.isTracking()
&& !event.isCanceled()) {
// if the call key is being released, AND we are tracking
// it from an initial key down, AND it is not canceled,
// then handle it.
return true;
}
return super.onKeyUp(keyCode, event);
}
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_0) {
-
-
-
- event.startTracking();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_0) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
- @Override
- public boolean dispatchKeyEvent(KeyEvent event) {
- if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
- if (event.getAction() == KeyEvent.ACTION_DOWN
- && event.getRepeatCount() == 0) {
-
-
- getKeyDispatcherState().startTracking(event, this);
- return true;
-
- } else if (event.getAction() == KeyEvent.ACTION_UP) {
- getKeyDispatcherState().handleUpEvent(event);
- if (event.isTracking() && !event.isCanceled()) {
-
-
- return true;
-
- }
- }
- return super.dispatchKeyEvent(event);
- } else {
- return super.dispatchKeyEvent(event);
- }
- }
阅读(1581) | 评论(0) | 转发(0) |