Android onKeyDown監(jiān)聽(tīng)返回鍵無(wú)效的解決辦法
Android onKeyDown監(jiān)聽(tīng)返回鍵無(wú)效的解決辦法
當(dāng)我們的Activity繼承了TabActivity,在該類中重寫onKeyDown是監(jiān)聽(tīng)不到返回鍵的,
具體解決方法如下:
重寫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); }
如果僅僅是監(jiān)聽(tīng)某個(gè)Activity的后退鍵,只需要覆寫該方法即可.
@Override public void onBackPressed() { super.onBackPressed(); }
我們可以看看super.onBackPressed()方法默認(rèn)的實(shí)現(xiàn):
/** * 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.
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android10 分區(qū)存儲(chǔ)的適配規(guī)則
Android設(shè)備 在Sdk29己以上推出了分區(qū)存儲(chǔ),類似iOS沙箱。應(yīng)用App 目標(biāo)版本號(hào)為29或以上則需要適配分區(qū)存儲(chǔ),本文將具體的講解適配規(guī)則2021-05-05Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)代碼
本篇文章對(duì)Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)用實(shí)例進(jìn)行了介紹。需要的朋友參考下2013-05-05Android?Studio實(shí)現(xiàn)登錄界面功能
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)登錄界面功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android實(shí)現(xiàn)多維商品屬性SKU選擇
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多維商品屬性SKU選擇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例
這篇文章主要介紹了Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11