废话不多说,直接上代码:
一、双击返回按钮退出登陆
long lastTime = 0;
/**
* 2秒内连续点击back键,退出应用
*/
@Override
public void onBackPressed() {
long curTime = System.currentTimeMillis();
if ((curTime - lastTime) > 2000) {
Toast.makeText(activity, "再按一次退出应用", Toast.LENGTH_SHORT).show();
lastTime = curTime;
} else {
this.finish();
}
}
二、弹出一个对话框
private AlertDialog.Builder builder;
builder= getDialog(context);
builder.setMessage("退出登陆!");
builder.setPositiveButton("确定", onOkClickListener);
builder.setNegativeButton("取消", onCancleClickListener);
@Override
public void onBackPressed() {
builder.show();
}