|
运行图:
代码如下:
[li]package cn.terry;[/li][li][/li][li]import android.app.Activity;[/li][li]import android.os.Bundle;[/li][li]import android.view.View;[/li][li]import android.view.View.OnClickListener;[/li][li]import android.widget.*;[/li][li]public class testGame extends Activity {[/li][li] private ImageView myImageView;[/li][li] private ImageView myImageView2;[/li][li] private ImageView myImageView3;[/li][li] private Button myButton;[/li][li] private int a=0;[/li][li] private int[] myImage={[/li][li] R.drawable.p01,[/li][li] R.drawable.p02,[/li][li] R.drawable.p03,[/li][li] };[/li][li][/li][li] private View.OnClickListener MyimageClistener;[/li][li] /** Called when the activity is first created. */[/li][li] @Override[/li][li] public void onCreate(Bundle savedInstanceState) {[/li][li] super.onCreate(savedInstanceState);[/li][li] setContentView(R.layout.main);[/li][li][/li][li] myImageView=(ImageView)findViewById(R.id.mImage01);[/li][li] myImageView2=(ImageView)findViewById(R.id.mImage02);[/li][li] myImageView3=(ImageView)findViewById(R.id.mImage03);[/li][li][/li][li] myButton=(Button)findViewById(R.id.mButton);[/li][li] Randon();[/li][li] MyimageClistener=new View.OnClickListener() {[/li][li][/li][li] @Override[/li][li] public void onClick(View v) {[/li][li][/li][li] // TODO Auto-generated method stub[/li][li] if(a==0)[/li][li] {[/li][li] switch (v.getId()) {[/li][li] case R.id.mImage01:[/li][li] SetImage(0,myImageView);[/li][li][/li][li] break;[/li][li] case R.id.mImage02:[/li][li] SetImage(1,myImageView2);[/li][li][/li][li] break;[/li][li] case R.id.mImage03:[/li][li] SetImage(2,myImageView3);[/li][li][/li][li] break;[/li][li] default:[/li][li] break;[/li][li] }[/li][li] }[/li][li] }[/li][li] };[/li][li][/li][li] myImageView.setOnClickListener(MyimageClistener);[/li][li] myImageView2.setOnClickListener(MyimageClistener);[/li][li] myImageView3.setOnClickListener(MyimageClistener);[/li][li][/li][li] myButton.setOnClickListener(new OnClickListener() {[/li][li][/li][li] @Override[/li][li] public void onClick(View v) {[/li][li] // TODO Auto-generated method stub[/li][li] if(a==1)[/li][li] {[/li][li] myImageView.setImageResource(R.drawable.p04);[/li][li] myImageView2.setImageResource(R.drawable.p04);[/li][li] myImageView3.setImageResource(R.drawable.p04);[/li][li] myImageView.setAlpha(255);[/li][li] myImageView2.setAlpha(255);[/li][li] myImageView3.setAlpha(255);[/li][li] Randon();[/li][li] a--;[/li][li] }[/li][li] }[/li][li] });[/li][li] }[/li][li][/li][li] private void SetImage(int temp,ImageView ss)[/li][li] {[/li][li] myImageView.setImageResource(myImage[0]);[/li][li] myImageView2.setImageResource(myImage[1]);[/li][li] myImageView3.setImageResource(myImage[2]);[/li][li] myImageView.setAlpha(100);[/li][li] myImageView2.setAlpha(100);[/li][li] myImageView3.setAlpha(100);[/li][li] ss.setAlpha(255);[/li][li] if(myImage[temp]==R.drawable.p01)[/li][li] {[/li][li] Toast.makeText(testGame.this, "恭喜你猜對了", Toast.LENGTH_SHORT)[/li][li] .show();[/li][li] }[/li][li] else[/li][li] {[/li][li] Toast.makeText(testGame.this, "對不起你猜錯了", Toast.LENGTH_SHORT)[/li][li] .show();[/li][li] }[/li][li] a++;[/li][li] }[/li][li][/li][li][/li][li][/li][li] private void Randon()[/li][li] {[/li][li] for(int i=0;i<myImage.length;i++)[/li][li] {[/li][li] int temp=myImage;[/li][li] int s=(int)(Math.random()*2);[/li][li] myImage=myImage[strike];[/strike][/li] |
|