java - 单击 Android 中的元素不会显示正确的值

标签 java android

如果这段代码看起来有点乱(考虑到长度),我深表歉意;我想我应该把目前程序中发生的所有事情都包括在内。

我正在尝试为 Android 创建一个相当简单的 Tic Tac Toe 应用程序。到目前为止,我已经很好地设置了我的 UI,以便有一个 TextView 的“网格”。作为一种“调试”,我现在拥有它,以便当单击 TextView 时,它应该在消息框中显示 buttonId 的值。现在,它显示我单击的第一个元素的正确分配值,但无论我之后单击什么,它总是只显示buttonID 的第一个值。我尝试对其进行调试,但无法准确找到提取旧值的点(据我所知,它重新分配了该值)。

我很有可能错过了一些小东西,因为这是我的第一个 Android 项目(无论如何)。有人可以帮助获取不同的 ButtonId 值来显示或指出我的逻辑中的错误吗?

代码:

package com.TicTacToe.app;

import com.TicTacToe.app.R;
//Other import statements

public class TicTacToe extends Activity {
public String player = "X";
public int ALERT_ID;
public int buttonId;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Sets up instances of UI elements
    final TextView playerText = (TextView)findViewById(R.id.CurrentPlayerDisp);
    final Button button = (Button) findViewById(R.id.SetPlayer);
    final TextView location1 = (TextView)findViewById(R.id.location1);
    final TextView location2 = (TextView)findViewById(R.id.location2);
    final TextView location3 = (TextView)findViewById(R.id.location3);
    final TextView location4 = (TextView)findViewById(R.id.location4);
    final TextView location5 = (TextView)findViewById(R.id.location5);
    final TextView location6 = (TextView)findViewById(R.id.location6);
    final TextView location7 = (TextView)findViewById(R.id.location7);
    final TextView location8 = (TextView)findViewById(R.id.location8);
    final TextView location9 = (TextView)findViewById(R.id.location9);


    playerText.setText(player);

    //Handlers for events
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            if (player.equals("X")){
                player = "O";
                playerText.setText(player);
            }
            else if(player.equals("O")){
                player = "X";
                playerText.setText(player);
            }

            //Sets up the dialog
            buttonId = 0;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);
        }
    });

    location1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 1;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 2;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 3;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 4;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 5;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 6;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 7;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 8;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 9;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });
}

protected Dialog onCreateDialog(int id){
    String msgString = "You are on spot " + buttonId;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(msgString)
            .setCancelable(false)
            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    }
                });
    AlertDialog alert = builder.create();
    return alert;
}

}

最佳答案

您不应该自己调用onCreateDialog()。当您调用 showDialog() 时,Android 会为您执行此操作。

我确信它只调用 onCreateDialog() 一次并在内部缓存该值,这就是为什么您只在第一次获得正确的值,然后为休息。

你必须想出另一种方法来完成你想做的事情。也许将不同的 ID 传递给 showDialog()(请确保通过在 onCreateDialog() 中创建正确的对话框来处理该 ID)是可行的方法。

另外,不要手动导入 R。

关于java - 单击 Android 中的元素不会显示正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2634422/

相关文章:

java - 计算当前浏览页面的用户数

android - 如何制作像 Lollipop 中的联系人应用程序一样的工具栏动画

带有按钮onClick事件的android自定义对话框

c# - 尝试使用 com.android.future.usb.UsbAccessory 和 UsbManager 命名空间

java - Liferay 博客 URL 和虚拟主机

java - Akka:使用非默认构造函数在 Scala 中定义一个 actor 并从 Java 代码创建它

java - @PreUpdate 不适用于 Spring Data JPA

java - REST Jackson JsonDeserialize,升级后出现 StackOverflowError

java - 如何在 GCM Java Message Builder 中设置 BigTextStyle

java - Android单例变为null