文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>引用 android的按键响应

引用 android的按键响应

时间:2010-08-25  来源:朱宇华

下面的是官方的bolg 觉得不错就引用了http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Java代码
  1. @Override  
  2. public boolean onKeyDown(int keyCode, KeyEvent event)  {  
  3.     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
  4.         // do something on back.  
  5.         return true;  
  6.     }  
  7.   
  8.     return super.onKeyDown(keyCode, event);  
  9. }  
@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);
}

 

Java代码
  1. @Override  
  2. public void onBackPressed() {  
  3. // do something on back.  
  4. return;  
  5. }  
@Override
public void onBackPressed() {
// do something on back.
return;
}

 

长按事件

Java代码
  1. @Override  
  2. public boolean onKeyLongPress(int keyCode, KeyEvent event) {  
  3.     if (keyCode == KeyEvent.KEYCODE_CALL) {  
  4.         // a long press of the call key.  
  5.         // do our work, returning true to consume it.  by  
  6.         // returning true, the framework knows an action has  
  7.         // been performed on the long press, so will set the  
  8.         // canceled flag for the following up event.  
  9.         return true;  
  10.     }  
  11.     return super.onKeyLongPress(keyCode, event);  
  12. }  
@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);
}

 

Java代码
  1. @Override  
  2. public boolean onKeyUp(int keyCode, KeyEvent event) {  
  3.     if (keyCode == KeyEvent.KEYCODE_CALL && event.isTracking()  
  4.             && !event.isCanceled()) {  
  5.         // if the call key is being released, AND we are tracking  
  6.         // it from an initial key down, AND it is not canceled,  
  7.         // then handle it.  
  8.         return true;  
  9.     }  
  10.     return super.onKeyUp(keyCode, event);  
  11. }  
@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);
}

 

Java代码
  1. @Override  
  2. public boolean onKeyDown(int keyCode, KeyEvent event)  {  
  3.     if (keyCode == KeyEvent.KEYCODE_0) {  
  4.         // this tells the framework to start tracking for  
  5.         // a long press and eventual key up.  it will only  
  6.         // do so if this is the first down (not a repeat).  
  7.         event.startTracking();  
  8.         return true;  
  9.     }  
  10.     return super.onKeyDown(keyCode, event);  
  11. }  
@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);
}

 

Java代码
  1. @Override  
  2. public boolean dispatchKeyEvent(KeyEvent event) {  
  3.     if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {  
  4.         if (event.getAction() == KeyEvent.ACTION_DOWN  
  5.                 && event.getRepeatCount() == 0) {  
  6.   
  7.             // Tell the framework to start tracking this event.  
  8.             getKeyDispatcherState().startTracking(event, this);  
  9.             return true;  
  10.   
  11.         } else if (event.getAction() == KeyEvent.ACTION_UP) {  
  12.             getKeyDispatcherState().handleUpEvent(event);  
  13.             if (event.isTracking() && !event.isCanceled()) {  
  14.   
  15.                 // DO BACK ACTION HERE  
  16.                 return true;  
  17.   
  18.             }  
  19.         }  
  20.         return super.dispatchKeyEvent(event);  
  21.     } else {  
  22.         return super.dispatchKeyEvent(event);  
  23.     }  

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载