我想做一個對話框按鈕,當我在主要活動時彈出並詢問用戶是否確定他們想關閉程序。我不知道要在「是」按鈕的「操作」中添加什麼內容......是否有更好的方法來執行此操作?確定/取消對話框後退按鈕
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Exit Game?");
// Icon for AlertDialog
alert.setIcon(R.drawable.icon);
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
調用完成()時,按下OK按鈕就可以做的伎倆 –
我會建議覆蓋'onBackPressed()'函數來處理特定的按鍵事件。 –