java Android程序不工作
我是安卓开发新手。我正在尝试创建一个tic-tac-toe游戏,目前我希望我的按钮在点击时被标记为X,但当我运行程序并点击一个按钮时,什么都没有发生。我的活动。java文件如下:
package com.安卓.tictactoe;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.*;
public class TictactoeActivity extends Activity implements Button.OnClickListener{
Button one, two, three, four, five, six, seven, eight, nine;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
one=(Button)findViewById(R.id.one);
one.setOnClickListener(this);
two=(Button)findViewById(R.id.two);
two.setOnClickListener(this);
three=(Button)findViewById(R.id.three);
three.setOnClickListener(this);
four=(Button)findViewById(R.id.four);
four.setOnClickListener(this);
five=(Button)findViewById(R.id.five);
five.setOnClickListener(this);
six=(Button)findViewById(R.id.six);
six.setOnClickListener(this);
seven=(Button)findViewById(R.id.seven);
seven.setOnClickListener(this);
eight=(Button)findViewById(R.id.eight);
eight.setOnClickListener(this);
nine=(Button)findViewById(R.id.nine);
nine.setOnClickListener(this);
}
public void OnClick(Button button, int CheckedId){
if(CheckedId==R.id.one && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.two && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.three && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.four && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.five && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.six && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.seven && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.eight && button.getText().equals("")){
button.setText("X");
}
button.setText("X");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
我的主菜。xml如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:stretchColumns="0" >
<TableRow>
<TextView
安卓:text="Fizzle TicTacToe"
安卓:textStyle="bold"
安卓:textSize="30dip"
安卓:textColor="#ffaa88"
/>
</TableRow>
<TableRow>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/one"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/two"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/three"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
</TableRow>
<TableRow>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/four"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/five"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/six"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
</TableRow>
<TableRow>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/seven"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/eight"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
<Button
安卓:textColor="#440000"
安卓:textSize="40dip"
安卓:textStyle="bold"
安卓:text=""
安卓:id="@+id/nine"
安卓:layout_span="1"
安卓:layout_width="0dip"
安卓:layout_height="100dip"
安卓:layout_weight="30"/>
</TableRow>
</TableLayout>
# 1 楼答案
你的OnClick方法无效,请用overide将他的内容复制到OnClick方法
# 2 楼答案
您需要在被重写的方法上提供
onClick
行为,而不是您自己的方法