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 と呼びます

このような onClick 呼び出しで

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

次のように、switch ステートメントを別のメソッドに記述します。

public void setRandomImages(int n)
{
//your switch statement here
}
于 2013-03-18T09:16:31.440 に答える