0

在此代码中,我想在切换条件下随机化或随机化我的 5 个案例*单击“正确”按钮后,图像将全屏显示并返回相同的案例。而不是这个,我想从剩余案例中加载图像随机的。*

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;
    }
    }

谢谢。

4

1 回答 1

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
}
于 2013-03-18T09:16:31.440 回答