Android初级教程_onKeyDown监听返回键无效
16lz
2021-01-24
当我们的Activity继承了TabActivity,在该类中重写onKeyDown是监听不到返回键的,
具体解决方法如下:
重写dispatchKeyEvent
/** * 退出 */@Overridepublic 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的后退键,只需要覆写该方法即可.
@Overridepublic void 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()方法注释即可
欢迎转载:http://blog.csdn.net/johnny901114/article/details/7822878
更多相关文章
- 安卓开发之去标题栏
- Android实现手机振动设置的方法
- 删除android默认APK的方法
- Android(安卓)BroadcastReceiver 简介
- android 访问SD卡的方法
- android之Unable to execute dex: Multiple dex files define
- android的apk包中的xml文件打开方法
- Android开发的小细节
- android 和JS互调