import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class XOGame extends Activity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
boolean btnFlag,theEnd;
private final static int RESTART = Menu.FIRST;
private final static int GO_BACK_TO_CHAT = Menu.FIRST+1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xogame);
btnFlag = true;
theEnd = false;
btn1 =(Button)findViewById(R.id.b1);
btn2 =(Button)findViewById(R.id.b2);
btn3 =(Button)findViewById(R.id.b3);
btn4 =(Button)findViewById(R.id.b4);
btn5 =(Button)findViewById(R.id.b5);
btn6 =(Button)findViewById(R.id.b6);
btn7 =(Button)findViewById(R.id.b7);
btn8 =(Button)findViewById(R.id.b8);
btn9 =(Button)findViewById(R.id.b9);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn1.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn1.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn1.setText("O");
}
}
GameIsOver();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn2.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn2.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn2.setText("O");
}
}
GameIsOver();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn3.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn3.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn3.setText("O");
}
}
GameIsOver();
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn4.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn4.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn4.setText("O");
}
}
GameIsOver();
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn5.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn5.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn5.setText("O");
}
}
GameIsOver();
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn6.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn6.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn6.setText("O");
}
}
GameIsOver();
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn7.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn7.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn7.setText("O");
}
GameIsOver();
}
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn8.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn8.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn8.setText("O");
}
GameIsOver();
}
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btn9.getText().toString().equals("")){
if(btnFlag == true){
btnFlag = false;
btn9.setText("X");
}
else if(btnFlag == false)
{
btnFlag = true;
btn9.setText("O");
}
}
GameIsOver();
}
});
}
public void GameIsOver(){
String btn11,btn22,btn33,btn44,btn55,btn66,btn77,btn88,btn99;
btn11 = btn1.getText().toString();
btn22 = btn2.getText().toString();
btn33 = btn3.getText().toString();
btn44 = btn4.getText().toString();
btn55 = btn5.getText().toString();
btn66 = btn6.getText().toString();
btn77 = btn7.getText().toString();
btn88 = btn8.getText().toString();
btn99 = btn9.getText().toString();
if(btn11.equals("X")&&btn22.equals("X")&&btn33.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn11.equals("X")&&btn55.equals("X")&&btn99.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn11.equals("X")&&btn44.equals("X")&&btn77.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn22.equals("X")&&btn55.equals("X")&&btn88.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn33.equals("X")&&btn66.equals("X")&&btn99.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn44.equals("X")&&btn55.equals("X")&&btn66.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn55.equals("X")&&btn33.equals("X")&&btn77.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn77.equals("X")&&btn88.equals("X")&&btn99.equals("X")){
Toast.makeText(XOGame.this,R.string.congrats_x_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
if(btn11.equals("O")&&btn22.equals("O")&&btn33.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn11.equals("O")&&btn55.equals("O")&&btn99.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn11.equals("O")&&btn44.equals("O")&&btn77.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn22.equals("O")&&btn55.equals("O")&&btn88.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn33.equals("O")&&btn66.equals("O")&&btn99.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn44.equals("O")&&btn55.equals("O")&&btn66.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn55.equals("O")&&btn33.equals("O")&&btn77.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(btn77.equals("O")&&btn88.equals("O")&&btn99.equals("O")){
Toast.makeText(XOGame.this,R.string.congrats_o_wins,Toast.LENGTH_SHORT).show();
theEnd = true;
}
if(theEnd == true) {
btn1.setClickable(false);
btn2.setClickable(false);
btn3.setClickable(false);
btn4.setClickable(false);
btn5.setClickable(false);
btn6.setClickable(false);
btn7.setClickable(false);
btn8.setClickable(false);
btn9.setClickable(false);
}
}
当我点击进入另一个活动的菜单时,应用程序崩溃了 //添加xo游戏的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
//adding menu options
menu.add(0, RESTART, 0, R.string.restart);
menu.add(0, GO_BACK_TO_CHAT, 0, R.string.go_back_to_chat);
return result;
}
我尝试了2个不同的案例,但它没有用 @覆盖 public boolean onMenuItemSelected(int featureId,MenuItem item) {
switch(item.getItemId())
{
case RESTART:
{
Intent i = new Intent(XOGame.this, XOGame.class);
startActivity(i);
return true;
}
/* case GO_BACK_TO_CHAT:
{
Intent i = new Intent(XOGame.this, WritingMessage.class);
startActivity(i);
return true;
}*/
/*case GO_BACK_TO_CHAT:
{
XOGame.this.finish();
return true;
}*/
}
return super.onMenuItemSelected(featureId, item);
}
}
答案 0 :(得分:1)
我通过在重新启动意图后完成意图来解决这个问题。
答案 1 :(得分:0)
可能是你在后台线程中设置文本,当它真的需要在主/ UI线程上完成时。 OnButtonPressed生成一个新线程,如果您需要更新任何UI,您需要执行以下操作。
"size": 5000
如果在后台线程中完成,则需要使用toast。