如何在Switch条件下随机化案例?

时间:2013-03-18 09:11:09

标签: android random switch-statement shuffle

在这段代码中,我想在切换条件下随机播放或随机化我的5个案例* 点击正确按钮后,图像显示全屏并返回到同一个案例。相反,我想要从Random。*

中加载剩余案例中的图像
public class MainActivity extends Activity{


ImageButton ib1,ib2,ib3,ib4,ib5,ib6,ib7,ib8,ib9,ib10,ib11,ib12,ib13,ib14,ib15;
ImageView iv;
Handler handler = new Handler();

int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv = (ImageView)findViewById(R.id.img);

    ib1 = (ImageButton)findViewById(R.id.imgbt1);
    ib2 = (ImageButton)findViewById(R.id.imgbt2);
    ib3 = (ImageButton)findViewById(R.id.imgbt3);
    ib4 = (ImageButton)findViewById(R.id.imgbt4);
    ib5 = (ImageButton)findViewById(R.id.imgbt5);
    ib6 = (ImageButton)findViewById(R.id.imgbt6);
    ib7 = (ImageButton)findViewById(R.id.imgbt7);
    ib8 = (ImageButton)findViewById(R.id.imgbt8);
    ib9 = (ImageButton)findViewById(R.id.imgbt9);
    ib10 = (ImageButton)findViewById(R.id.imgbt10);
    ib11 = (ImageButton)findViewById(R.id.imgbt11);
    ib12 = (ImageButton)findViewById(R.id.imgbt12);
    ib13 = (ImageButton)findViewById(R.id.imgbt13);
    ib14 = (ImageButton)findViewById(R.id.imgbt14);
    ib15 = (ImageButton)findViewById(R.id.imgbt15);




    Random rand = new Random();
    x = rand.nextInt(5);

    switch (x) {
    case 1:

        Toast.makeText(getApplicationContext(), "Select red", Toast.LENGTH_SHORT).show();
        ib1.setVisibility(View.VISIBLE);
        ib1.setBackgroundResource(R.drawable.carrot);

        //ib1.setOnClickListener(myListener);
        ib1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;

                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.carrot);

            }
        });

        ib2.setVisibility(View.VISIBLE);
        ib2.setBackgroundResource(R.drawable.ic_launcher);
        //ib2.setOnClickListener(myListener);


        ib3.setVisibility(View.VISIBLE);
        ib3.setBackgroundResource(R.drawable.ic_launcher);
        //ib3.setOnClickListener(myListener);


        break;

    case 2:
        ib4.setVisibility(View.VISIBLE);
        ib4.setBackgroundResource(R.drawable.ic_launcher);
        ib4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.ic_launcher);
                //ib4.setOnClickListener(myListener);


            }
        });



        ib5.setVisibility(View.VISIBLE);
        ib5.setBackgroundResource(R.drawable.ic_launcher);
        //ib5.setOnClickListener(myListener);


        ib6.setVisibility(View.VISIBLE);
        ib6.setBackgroundResource(R.drawable.ic_launcher);
        //ib6.setOnClickListener(myListener);


        break;


    case 3:
        Toast.makeText(getApplicationContext(), "Select Violet", Toast.LENGTH_SHORT).show();

        ib7.setVisibility(View.VISIBLE);
        ib7.setBackgroundResource(R.drawable.brinjal);
        //ib7.setOnClickListener(myListener);
        ib7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.brinjal);
            }
        });


        ib8.setVisibility(View.VISIBLE);
        ib8.setBackgroundResource(R.drawable.ic_launcher);
        //ib8.setOnClickListener(myListener);


        ib9.setVisibility(View.VISIBLE);
        ib9.setBackgroundResource(R.drawable.ic_launcher);
        //ib9.setOnClickListener(myListener);


        break;

    case 4:

        Toast.makeText(getApplicationContext(), "Select Any thing", Toast.LENGTH_SHORT).show();

        ib10.setVisibility(View.VISIBLE);
        ib10.setBackgroundResource(R.drawable.ic_launcher);
        //ib10.setOnClickListener(myListener);
        ib10.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.ic_launcher);

            }
        });


        ib11.setVisibility(View.VISIBLE);
        ib11.setBackgroundResource(R.drawable.ic_launcher);
        //ib11.setOnClickListener(myListener);


        ib12.setVisibility(View.VISIBLE);
        ib12.setBackgroundResource(R.drawable.ic_launcher);
        //ib12.setOnClickListener(myListener);


        break;



    case 5:
        Toast.makeText(getApplicationContext(), "Select Blue", Toast.LENGTH_SHORT).show();

        ib13.setVisibility(View.VISIBLE);
        ib13.setBackgroundResource(R.drawable.blue_bird);
        //ib13.setOnClickListener(myListener);

        ib13.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.blue_bird);

            }
        });





        ib14.setVisibility(View.VISIBLE);
        ib14.setBackgroundResource(R.drawable.ic_launcher);
        //ib14.setOnClickListener(myListener);


        ib15.setVisibility(View.VISIBLE);
        ib15.setBackgroundResource(R.drawable.ic_launcher);
        //ib15.setOnClickListener(myListener);


        break;          

    default:
        break;
    }
    }

感谢。

1 个答案:

答案 0 :(得分:2)

因为Button onclick只会执行我们在onClick中给出的代码,所以你没有在按钮的onClick中调用你的switch语句。

以单独的方式调用Switch语句,并在按钮上调用它

在你的onClick调用上

就像这样

int num= rand.nextInt(5);
setRandomImages(num)

将switch语句写入一个单独的方法,如

public void setRandomImages(int n)
{
//your switch statement here
}