当“新游戏”出现时显示对话框'单击按钮

时间:2012-03-02 19:32:46

标签: android

我正在创建一个游戏,其中我在对话框中获得了难度级别的代码,但我无法理解如何在单击“新游戏”按钮时显示难度级别的对话框。我在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 个答案:

答案 0 :(得分:2)

您似乎没有将AlertDialog对象分配给变量来分配属性方法和show()函数。请查看此处有关AlertDialog的文档,了解您的方法的差异:

所以当你说

new AlertDialog.Builder(this);

它应该有一个变量来将此对象分配给:

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

然后您将构建器对象用于setTitlesetItemsshow

答案 1 :(得分: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();
}