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

时间:2010-04-14 02:03:59

标签: java android

我很抱歉,如果这段代码看起来有点混乱(考虑到长度);我想我现在只包括我程序中发生的所有事情。

我正在尝试为Android创建一个相当简单的Tic Tac Toe应用程序。到目前为止,我已经很好地设置了我的UI,以便有一个TextViews的“网格”。作为一种“调试”现在,我有它,所以当一个单击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;
}

}

1 个答案:

答案 0 :(得分:1)

您不应该自己致电onCreateDialog()。当您拨打showDialog()时,Android会为您执行此操作。

我确定它只调用onCreateDialog()一次并在内部缓存该值,这就是为什么你只是第一次获得正确的值,然后为其余值获得相同的值。

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

另外,请勿手动导入R.