先看下onBackPressed和onKeyDown的区别
在Android上有两种方法来获取该按钮的事件
1.直接获取按钮按下事件,此方法兼容Android 1.0到Android 2.1 也是常规方法,直接重写Activity的onKeyDown方法即可,代码如下:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { //按下的如果是BACK,同时没有重复
Toast.makeText(ml78.this,"返回键Back键测试",1).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
而对于Android 2.0开始又多出了一种新的方法,对于Activity 可以单独获取Back键的按下事件,直接重写onBackPressed方法即可,代码如下
@Override
public void onBackPressed() {
// 这里处理逻辑代码,大家注意:该方法仅适用于2.0或更新版的sdk
return;
}
1 现在实现对根Activity按下2此返回键实现退出
private static boolean isSysExit = false; @Override public void onBackPressed() { // TODO Auto-generated method stub if(!isSysExit){ isSysExit = true; DensityUtil.showToast(this, "再按一次退出"); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub isSysExit =false; } }, 2000); }else { finish(); } }
2 在根Activity中重写后退按钮响应事件,当按后退按钮的时候把Activity退置到后台
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub //当按下返回键时吧Activity退至到后台 if(keyCode ==KeyEvent.KEYCODE_BACK){ moveTaskToBack(true); return true; } return super.onKeyDown(keyCode, event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } else { mDrawerLayout.openDrawer(Gravity.LEFT); } return true; } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } else { if ((System.currentTimeMillis() - DOUBLE_CLICK_TIME) > 2000) { showToast(getString(R.string.double_click_exit)); DOUBLE_CLICK_TIME = System.currentTimeMillis(); } else { getBaseApplication().exitApp(); } } return true; } return super.onKeyDown(keyCode, event); }
获取时间从http://blog.csdn.net/kookob/article/details/6885383粘贴来的
package com.ob; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateTest { public static void main(String[] args) throws ParseException { Calendar now = Calendar.getInstance(); System.out.println("年: " + now.get(Calendar.YEAR)); System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + ""); System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分: " + now.get(Calendar.MINUTE)); System.out.println("秒: " + now.get(Calendar.SECOND)); System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); System.out.println(now.getTime()); Date d = new Date(); System.out.println(d); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateNowStr = sdf.format(d); System.out.println("格式化后的日期:" + dateNowStr); String str = "2012-1-13 17:26:33"; //要跟上面sdf定义的格式一样 Date today = sdf.parse(str); System.out.println("字符串转成日期:" + today); } }
输出结果:
年: 2012
月: 1
日: 13
时: 17
分: 28
秒: 19
当前时间毫秒数:1326446899902
Fri Jan 13 17:28:19 CST 2012
Fri Jan 13 17:28:19 CST 2012
格式化后的日期:2012-01-13 17:28:19
字符串转成日期:Fri Jan 13 17:26:33 CST 2012