我希望从 5 个图像视图中随机显示一个图像视图,然后当用户单击显示的图像视图时,我希望出现另一个图像视图,并且该过程将重复直到计数达到 5,但代码一次显示多个图像视图。
我的代码:
public class Gamepage extends Activity
{
ImageView img,img1,img2,img3,img4;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.trygame);
img=(ImageView) findViewById(R.id.imageView2);
img1=(ImageView) findViewById(R.id.imageView3);
img2=(ImageView) findViewById(R.id.imageView4);
img3=(ImageView) findViewById(R.id.imageView5);
img4=(ImageView) findViewById(R.id.imageView6);
int choose;
int count=0;
choose=(int) (Math.random()*5);
while(count!=5)
{
switch(choose)
{
case 1:
img.setVisibility(View.VISIBLE);
img.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
//count++;
break;
case 2:
img1.setVisibility(View.VISIBLE);
img1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img1.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
//count++;
break;
case 3:
img2.setVisibility(View.VISIBLE);
img2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img2.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
//count++;
break;
case 4:
img3.setVisibility(View.VISIBLE);
img3.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img3.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
//count++;
break;
case 5:
img4.setVisibility(View.VISIBLE);
img4.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img4.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
// count++;
break;
default:
img4.setVisibility(View.VISIBLE);
img4.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
toast.setGravity(Gravity.RIGHT, 10,10);
toast.show();
img4.setVisibility(View.INVISIBLE);
return true;
}
});
//choose=(int) (Math.random()*5);
//count++;
break;
}
choose=(int) (Math.random()*5);
count++;
}
}
}