只是一个简单的猜拳游戏,原来用普通Button没问题,但是换了ImageButton加了图片以后就崩溃了,是不是因为图片的问题?package com.example.caiquan;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private TextView result; private ImageView computer; private Button quan; private Button jiandao; private Button bu; private void init() { quan = (Button) findViewById(R.id.quan); jiandao = (Button) findViewById(R.id.jiandao); bu = (Button) findViewById(R.id.bu); result = (TextView) findViewById(R.id.result); computer = (ImageView) findViewById(R.id.imageview); quan.setOnClickListener(chuquan); jiandao.setOnClickListener(chujiandao); bu.setOnClickListener(chubu); } private Button.OnClickListener chuquan =new View.OnClickListener() { @Override public void onClick(View view) { //随机数决定计算机出拳 int icom = (int) (Math.random()*3+1); //1--拳 2--剪刀 3--布 if (icom==1){ computer.setImageResource(R.mipmap.stone); result.setText(getText(R.string.panduan)+getString(R.string.draw)); } if (icom==2){ computer.setImageResource(R.mipmap.jiandao); result.setText(getText(R.string.panduan)+getString(R.string.win)); } if (icom==3){ computer.setImageResource(R.mipmap.bu); result.setText(getText(R.string.panduan)+getString(R.string.lose)); } } }; private Button.OnClickListener chujiandao =new View.OnClickListener() { @Override public void onClick(View view) { //随机数决定计算机出拳 int icom = (int) (Math.random()*3+1); //1--拳 2--剪刀 3--布 if (icom==1){ computer.setImageResource(R.mipmap.stone); result.setText(getText(R.string.panduan)+getString(R.string.lose)); } if (icom==2){ computer.setImageResource(R.mipmap.jiandao); result.setText(getText(R.string.panduan)+getString(R.string.draw)); } if (icom==3){ computer.setImageResource(R.mipmap.bu); result.setText(getText(R.string.panduan)+getString(R.string.win)); } } }; private Button.OnClickListener chubu =new View.OnClickListener() { @Override public void onClick(View view) { //随机数决定计算机出拳 int icom = (int) (Math.random()*3+1); //1--拳 2--剪刀 3--布 if (icom==1){ computer.setImageResource(R.mipmap.stone); result.setText(getText(R.string.panduan)+getString(R.string.win)); } if (icom==2){ computer.setImageResource(R.mipmap.jiandao); result.setText(getText(R.string.panduan)+getString(R.string.lose)); } if (icom==3){ computer.setImageResource(R.mipmap.bu); result.setText(getText(R.string.panduan)+getString(R.string.draw)); } } };}
添加回答
举报
0/150
提交
取消