其实我觉得吧,自带的方法还是直接放代码比较简单粗暴来的实在,真的!
代码实例:
package com.zking.laci.android13;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private long time=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
long times=System.currentTimeMillis();
if(times-time>2000){
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
time=times;
return true;
}else{
System.exit(0);
}
break;
}
return super.onKeyUp(keyCode, event);
}
}