2011-12-19 33 views
0

我有一個android遊戲,當用戶獲勝時,它會顯示一個對話框,詢問他們是否要再次玩。如果他們說「不」,則應用程序退出,如果他們說「是」,則開始另一個遊戲。問題在於新的遊戲在顯示對話框時開始,而不是等待用戶按下按鈕。代碼是這樣的:想要android應用程序等待,直到用戶按下按鈕,然後繼續

if (won) { 
    showDialog(DIALOG_WON_ID); 
    imageAdapter.initializemThumbIds(); // this starts a new game 
} 

我不希望開始新的遊戲線執行,直到對話框被解散。

回答

2

將這個呼叫

imageAdapter.initializemThumbIds(); 

進入 「是」 按鈕的點擊事件。

編輯:

事情是這樣的:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Would you like to play again?") 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       imageAdapter.initializemThumbIds(); 
      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // Exit the game 
      } 
     }); 
AlertDialog alert = builder.create(); 
alert.show(); 
+0

它說「imageAdapter無法解析」。 – BenH 2011-12-19 02:36:31

+0

他的意思是「if(won){showDialog}」,然後爲對話框按鈕設置一個onclick監聽器,並使對話框按鈕啓動新遊戲。 – 2011-12-19 03:06:25

+0

也可以爲你的方法使用更多的描述性名字'imageAdapter.initializemThumbIds()'聽起來不像是開始一個新遊戲。 – 2011-12-19 03:06:52

1

您當前的代碼顯示的對話框,並立即進入線以下showDialog()。你想要做的是將開始一個新遊戲的代碼移動到showDialog()中實現的DialogDialogInterface.OnClickListener()代碼。

相關問題