2012-03-02 81 views
1

即時通訊創建一個遊戲,其中我已經在對話框中獲得難度級別的代碼,但是我無法理解如何顯示新遊戲按鈕時的難度級別對話框被點擊。我已經在array.xml和string.xml中獲得了難度級別的代碼。點擊「新建遊戲」按鈕時顯示對話框

arrays.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
     <array name="difficulty"> 
      <item>@string/novice_label</item> 
      <item>@string/easy_label</item> 
      <item>@string/medium_label</item> 
      <item>@string/guru_label</item> 
     </array> 
    </resources> 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    <string name="app_name">Brain Training Game</string> 
    <string name="main_title">Brain Training Game</string> 
    <string name="new_game_label">New Game</string> 
    <string name="continue_label">Continue</string> 
    <string name="about_label">About</string> 
    <string name="exit_label">Exit</string> 
    <string name="about_title">About Brain Training Game</string> 
    <string name="about_text">\ 
    Brain Training Game is a logic-based number placement puzzle. 
    Starting with a partially completed 9x9 grid, the 
    objective is to fill the grid so that each 
    row, each column, and each of the 3x3 boxes 
    (also called <i>blocks</i>) contains the digits 
    1 to 9 exactly once</string> 
    <string name="new_game_title">Difficulty</string> 
    <string name="novice_label">Novice</string> 
    <string name="easy_label">Easy</string> 
    <string name="medium_label">Medium</string> 
    <string name="guru_label">Guru</string> 
    </resources> 

Btg.Java

package org.example.btg; 

import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.util.Log; 

import android.os.Bundle; 

public class BrainTrainingGame extends Activity implements OnClickListener { 
    protected static final String TAG = null; 
    /** Called when the activity is first created. */ 
    @SuppressWarnings("null") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     View newButton = findViewById(R.id.new_button); 
     newButton.setOnClickListener(this); 
     View continueButton = findViewById(R.id.continue_button); 
     continueButton.setOnClickListener(this); 
     View aboutButton = findViewById(R.id.about_button); 
     aboutButton.setOnClickListener(this); 
     View exitButton = findViewById(R.id.exit_button); 
     exitButton.setOnClickListener(this); 
    } 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.about_button: 
     Intent i = new Intent(this, About.class); 
     startActivity(i); 
     break; 

     case R.id.exit_button: 
     finish(); 
     break; 

     case R.id.new_button: 
     openNewGameDialog(); 
     break; 
     } 
    } 


    public void openNewGameDialog() { 
     new AlertDialog.Builder(this); 
     setTitle(R.string.new_game_title); 
     setItems(R.array.difficulty,new DialogInterface.OnClickListener() 
     new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialoginterface, int i) { 
       startGame(i); 
      } 
     }) 
     .show(); 
} 

    private void startGame(int i){ 
     Log.d(TAG, "clicked on " +i); 

    } 
} 

感謝提前幫助;)

回答

2

看來你不分配AlertDialog對象變量來分配財產的方法和show()功能。看看文檔這裏AlertDialog在你的方法的差異:

所以,當你說

new AlertDialog.Builder(this); 

它應該有一個變量來指定這個對象:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

然後您可以使用構建器對象setTitle,setItemsshow

+0

感謝隊友..它工作!!!!!!!!!!!!!!!!! ! :d – 2012-03-02 20:22:53

1

目前,我不相信這段代碼編譯,但也許這就是你的問題所在。

您用於創建對話框的代碼稍有缺陷。您正在嘗試使用AlertDialog.Builder,但您沒有提及它,因此對此沒有做任何處理。我看到你可以做兩件事來緩解這種情況。

您可以刪除openNewGameDialog()方法前兩行中的分號,並用點運算符替換它們。這將修復它,因爲AlertDialog.Builder使用鏈接(其設置方法返回this,以便您可以將多個方法'鏈接在一起)。

public void openNewGameDialog() { 
    new AlertDialog.Builder(this) 
    .setTitle(R.string.new_game_title) 
    .setItems(R.array.difficulty,new DialogInterface.OnClickListener() 
    new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialoginterface, int i) { 
      startGame(i); 
     } 
    }) 
    .show(); 
} 

否則你可以堅持實例,並不斷調用你的setter實例。

public void openNewGameDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(R.string.new_game_title); 
    builder.setItems(R.array.difficulty,new DialogInterface.OnClickListener() 
    new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialoginterface, int i) { 
      startGame(i); 
     } 
    }); 
    builder.show(); 
} 
+0

它工作!!!!!!!!感謝很多隊友:嗯嗯,你能解釋一下第一塊代碼的每一行代表什麼意思在評論中加入plzzzzz – 2012-03-02 20:19:05

相關問題