Android back鍵操作

1.重點程式碼
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this, 1)
.setMessage("Are you leaving the application?")
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {}}).setPositiveButton("Yes",new DialogInterface.OnClickListener(){ 
public void onClick(DialogInterface dialog,int whichButton) {
           finish(); 
}}).show(); 
}
return super.onKeyDown(keyCode, event);
}

沒有留言: