只是一個簡單的猜拳游戲,原來用普通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) { ? ? ? ? ? ?//隨機數(shù)決定計算機出拳 ? ? ? ? ? ?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) { ? ? ? ? ? ?//隨機數(shù)決定計算機出拳 ? ? ? ? ? ?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) { ? ? ? ? ? ?//隨機數(shù)決定計算機出拳 ? ? ? ? ? ?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
提交
取消