标签:android style blog http color io ar sp div
From:http://blog.csdn.net/ldj299/article/details/7574365
个人觉得当用户按下后退键时,出现"再按一次退出"的提示防止误操作比弹出一个对话框是会更人性化的。话不多说上代码:
第一种:重写onKeyDown来监听:
long waitTime = 2000; long touchTime = 0; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) { long currentTime = System.currentTimeMillis(); if((currentTime-touchTime)>=waitTime) { Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); touchTime = currentTime; }else { finish(); } return true; } return super.onKeyDown(keyCode, event); }
第二种是重写onBackPressed方法直接监听返回键:
@Override public void onBackPressed() { // super.onBackPressed(); long currentTime = System.currentTimeMillis(); if((currentTime - touchTime) >= waitTime) { Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); touchTime = currentTime; }else { finish(); } }
标签:android style blog http color io ar sp div
原文地址:http://www.cnblogs.com/duanguyuan/p/4001050.html