當我們的Activity繼承了TabActivity,在該類中重寫onKeyDown是監聽不到返回鍵的,
具體解決方法如下:
重寫dispatchKeyEvent
/** * 退出 */ @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { //具體的操作代碼 } return super.dispatchKeyEvent(event); }
---------------------------------------------------------------后續2012-8-23-----------------------------------------
如果僅僅是監聽某個Activity的后退鍵,只需要覆寫該方法即可.
@Override public void onBackPressed() { super.onBackPressed(); }
我們可以看看super.onBackPressed()方法默認的實現:
/** * Called when the activity has detected the user's press of the back * key. The default implementation simply finishes the current activity, * but you can override this to do whatever you want. */ public void onBackPressed() { finish(); }
如果想屏蔽后退鍵只需要把super.onBackPressed()方法注釋即可
但是該方法不適用於TabActivity.
歡迎轉載:http://blog.csdn.net/johnny901114/article/details/7822878
