当前位置: 代码迷 >> Android >> android中回来键事件
  详细解决方案

android中回来键事件

热度:78   发布时间:2016-05-01 17:07:19.0
android中返回键事件

在android中重写父类的onkeydown方法,就可以更方便的操作了。

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){	//需要处理}	return false;}

?长按事件

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

?

?

@Overridepublic 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);}@Overridepublic 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);}@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {        if (event.getAction() == KeyEvent.ACTION_DOWN                && event.getRepeatCount() == 0) {            // Tell the framework to start tracking this event.            getKeyDispatcherState().startTracking(event, this);            return true;        } else if (event.getAction() == KeyEvent.ACTION_UP) {            getKeyDispatcherState().handleUpEvent(event);            if (event.isTracking() && !event.isCanceled()) {                // DO BACK ACTION HERE                return true;            }        }        return super.dispatchKeyEvent(event);    } else {        return super.dispatchKeyEvent(event);    }}

?

  相关解决方案