【发布时间】:2017-02-25 18:00:53
【问题描述】:
我在 3 X 3 网格中有九个按钮,用于井字游戏应用程序。这是 board_layout.xml 中前三个按钮的代码 -
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/cellOne"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
<Button
android:id="@+id/cellTwo"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
<Button
android:id="@+id/cellThree"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
</TableRow>
<!and so on for other 6>
现在在我的 MainActivity.java 中,我有一个函数 setboard(),可以在此屏幕上设置此布局并按如下方式初始化按钮:
private String p1name = "P1";
private String p2name = "P2";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setName(View view) {
//Function called upon click on some button
// Other code irrelevant to the question
setBoard();
}
}
public void setBoard() {
////////////
LayoutInflater inflater = this.getLayoutInflater();
View boardView = inflater.inflate(R.layout.board_layout, null);
////////////
boardCells = new Button[3][3];
boardCells[0][0] = (Button) boardView.findViewById(R.id.cellOne);
boardCells[0][1] = (Button) boardView.findViewById(R.id.cellTwo);
boardCells[0][2] = (Button) boardView.findViewById(R.id.cellThree);
// Code for other 6 buttons
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
boardCells[i][j].setOnClickListener(new MyClickListener(i, j));
boardCells[i][j].setText("");
boardCells[i][j].setEnabled(false);
}
setContentView(R.layout.board_layout);
TextView tv1 = (TextView) findViewById(R.id.p1Name_board);
tv1.setText(p1name + ":");
tv1 = (TextView) findViewById(R.id.p2Name_board);
tv1.setText(p2name + ":");
turnDisp = (TextView) findViewById(R.id.turnDispString);
turnDisp.setText("Turn of " + p1name);
}
下面是 MyClickListener 类的代码(MainActivity 类的内部类):
class MyClickListener implements View.OnClickListener {
int x;
int y;
public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
//This Log.d works fine
Log.d("TAG1", Float.toString(x) + " " + Float.toString(y));
}
public void onClick(View view) {
//This Log.d doesn't work
Log.d("TAG2", Float.toString(this.x) + " Hi ");
//This Toast also doesn't work
Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG).show();
//Plus Other code irrelevant to the question
}
现在的问题是在设置按钮时调用的构造函数中的 Log.d 工作正常并打印到 Logcat 但单击按钮时 onClick 函数不起作用。 onClick 函数既不显示 Log.d 也不显示 toast。
任何帮助将不胜感激。
【问题讨论】:
-
数组索引为零,顺便说一句
-
inflate()电话的其余部分是什么?您是否将板直接充气到现有布局中?如果没有,您在哪里将充气板添加到现有布局中?你确定你设置监听器的Views 实际上是屏幕上的板吗? -
@cricket_007 我知道这只是为了方便。而且我不太了解 Java,所以请详细说明 Override。
-
关于迈克所说的,
View boardView = inflater.inflate。那行不完整....boardView稍后在该代码中如何使用?你是如何显示这些按钮的? -
我们需要查看整个 setBoard() 方法以及调用它的整个方法,我假设它是一个 onCreate(),不要遗漏部分内容,您可能会遗漏一些东西
标签: java android android-layout