[問題] 返回鍵離開APP的寫法

看板AndroidDev作者 (ㄚ達)時間13年前 (2011/11/15 12:12), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
請教一個問題 當APP啟動後 會在Activity上 秀出一個Dialog 我想請問的是 如果我今天 在Dialog的畫面還存在時 我按下手機的"返回鍵" 怎樣讓他直接連同activity也關閉 以下是大致的程式碼 http://nopaste.csie.org/c788c public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final AlertDialog alertDialog = getAlertDialog(title,message); alertDialog.show();//顯示對話框 } private AlertDialog getAlertDialog(String title,String message){ builder.setPositiveButton("Start", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { }}); builder.setNegativeButton("Stop", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); return builder.create(); } 現在欲加上 public boolean onKeyDown(int keyCode, KeyEvent event){ if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){ Client_v1Activity.this.finish(); return true; } return super.onKeyDown(keyCode, event); } 但是我現在卡在 我不知道要放在哪裡呼叫 onKeyDown(KeyEvent.KEYCODE_BACK,null); 還是應該是要有其他的寫法? 感謝回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.177.109

11/15 12:21, , 1F
你按下按鍵的時候 super那邊就會call過來了 ...
11/15 12:21, 1F

11/15 12:21, , 2F
所以你要override 看起來是沒有基本概念所產生的問題
11/15 12:21, 2F

11/16 16:52, , 3F
@Override
11/16 16:52, 3F

11/16 16:53, , 4F
public void onBackPressed() {}
11/16 16:53, 4F

11/17 13:48, , 5F
推樓上,建議用新API唷
11/17 13:48, 5F
文章代碼(AID): #1EmUQ__o (AndroidDev)
文章代碼(AID): #1EmUQ__o (AndroidDev)