【发布时间】:2010-04-14 02:03:59
【问题描述】:
如果这段代码看起来有点乱(考虑到长度),我深表歉意;我想我现在只需要包含我的程序中发生的所有事情。
我正在尝试为 Android 创建一个相当简单的井字游戏应用程序。到目前为止,我已经很好地设置了我的 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;
}
}
【问题讨论】: