Android onKeyDown監聽返回鍵無效的解決辦法
當我們的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); } 如果僅僅是監聽某個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.
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答