2012-03-02 12 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

あなたの財産法とshow()機能を割り当てるために、変数にAlertDialogオブジェクトを割り当てていないようです。その後、

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

:それはこのオブジェクトを割り当てるための変数を持っている必要があり

new AlertDialog.Builder(this); 

ので

あなたが言う:あなたのアプローチの違いをAlertDialogはこちらのドキュメントを見てみましょうBuilderオブジェクトをsetTitle,setItemsおよびshowに使用します。

+0

ありがとうございました..それは働きました!!!!!!!!!!!!!!!!! !!! :D –

1

現在、私はこのコードがコンパイルされることを納得していないけど、おそらくそれはあなたの問題です。

ダイアログを作成する際に使用しているコードに若干の欠陥があります。 AlertDialog.Builderを使用しようとしていますが、参照を保持していないため、何もしていません。私はあなたがこれを軽減するためにできる2つのことを見ています。

openNewGameDialog()メソッドの最初の2行でセミコロンを削除し、ドット演算子で置き換えることができます。これは、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(); 
} 

そうでなければ、インスタンスを保持してインスタンス上にセッターを呼び出すことができます。

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

it worked !!!!!!!!おかげでたくさんの仲間になりました:Dええとあなたは私に説明できますか?コードの最初のブロックのすべての行はコメントに追加することを意味します。plzzzzz –

関連する問題