Android中某個類繼承Activity的子類TabActivity時,重載Activity中的public boolean onKeyDown(int keyCode, KeyEvent event)方法時,點擊按鍵時並得不到響應,解決的方法是:
重載public boolean dispatchKeyEvent(KeyEvent event)方法,其示例代碼如下:
- @Override
- public boolean dispatchKeyEvent(KeyEvent event) {
- if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
- if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
- 響應事件的具體代碼
- }
- return true;
- }
- return super.dispatchKeyEvent(event);
- }