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

相关问题