效果图如下:
代码如下:
package androidstudio.androidexame_13;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private Boolean b = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
exit();
}
return false;
}
public void exit(){
if(b==false){
b=true;
Toast.makeText(MainActivity.this,"再次点击退出",Toast.LENGTH_SHORT).show();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
b= false;
}
},3000);
}else {
finish();
System.exit(0);
}
}
}
xml布局文件不用写什么,直接默认创建的就行。 如有错误请指出,谢谢。