Android 幾種常用的返回鍵重寫方式


// 第一種
public boolean onKeyDown(int keyCode, KeyEvent event) {

// 按下鍵盤上返回按鈕 
if (keyCode == KeyEvent.KEYCODE_BACK) {

new AlertDialog.Builder(this)
.setMessage("確定退出系統嗎?")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
}).show();

return true;
} else {
return super.onKeyDown(keyCode, event);
}

}

//直接退出程序
@Override
protected void onDestroy() {
  super.onDestroy();
  // 或者下面這種方式
  //System.exit(0);
  //建議用這種
  android.os.Process.killProcess(android.os.Process.myPid());
}

 

// 第二種
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    return true;
  }
  return super.onKeyDown(keyCode, event);
}

 

// 第三種
@Override
public void onBackPressed() {
  super.onBackPressed();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM