onClickListner for for循环不起作用

时间:2016-05-20 09:10:16

标签: java android for-loop increment

变量i没有递增,如果答案是正确的,那么我想通过递增变量i来提出另一个问题。我实现了OnClickListner但没有发生任何事情。我是android的新手,谢谢。

public class MainActivity extends AppCompatActivity{

        ImageView car, icecream, pencil, umbrella, ball;
        TextView question, answer;
        int i, size, j;
        final String[] objects  = {"car", "icecream", "pencil", "ball","umbrella"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

           final Animation expandIn = AnimationUtils.loadAnimation(this, R.anim.expand_in);




            size = objects.length;
            car = (ImageView) findViewById(R.id.car);

            umbrella = (ImageView) findViewById(R.id.umbrella);

            icecream = (ImageView) findViewById(R.id.icecream);

            pencil = (ImageView) findViewById(R.id.pencil);

            ball = (ImageView) findViewById(R.id.ball);

            question = (TextView) findViewById(R.id.question);
            answer = (TextView) findViewById(R.id.answer);

            j=0;
            for (i=0; i < size; i++)

            {

                if(i == j){
                    question.setText("");
                    question.setText("Where is "+objects[i]+" ?");
                    car.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            answer.setVisibility(View.VISIBLE);
                            if("car"==objects[i]){
                                answer.setText("Right Answer");
                                answer.setTextColor(Color.parseColor("#008000"));
                                j++;
                                car.startAnimation(expandIn);


                            } else {
                                answer.setText("Wrong Answer");
                                answer.setTextColor(Color.RED);

                                answer.postDelayed(new Runnable() {
                                    public void run() {
                                        answer.setVisibility(View.INVISIBLE);
                                    }
                                }, 2000);

                            }

                        }
                    });

                    icecream.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            answer.setVisibility(View.VISIBLE);
                            if("icecream"==objects[i]){
                                answer.setText("Right Answer");
                                j++;

                            } else {
                                answer.setText("Wrong Answer");
                                answer.setTextColor(Color.RED);

                                answer.postDelayed(new Runnable() {
                                    public void run() {
                                        answer.setVisibility(View.INVISIBLE);
                                    }
                                }, 2000);

                            }

                        }
                    });

                    pencil.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            answer.setVisibility(View.VISIBLE);
                            if("pencil"==objects[i]){
                                answer.setText("Right Answer");
                                j++;
                            } else {
                                answer.setText("Wrong Answer");
                                answer.setTextColor(Color.RED);

                                answer.postDelayed(new Runnable() {
                                    public void run() {
                                        answer.setVisibility(View.INVISIBLE);
                                    }
                                }, 2000);

                            }

                        }
                    });


                    ball.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            answer.setVisibility(View.VISIBLE);
                            if("ball"==objects[i])

                            {
                                answer.setText("Right Answer");
                                j++;

                            } else {
                                answer.setText("Wrong Answer");
                                answer.setTextColor(Color.RED);

                                answer.postDelayed(new Runnable() {
                                    public void run() {
                                        answer.setVisibility(View.INVISIBLE);
                                    }
                                }, 2000);

                            }

                        }
                    });


                    umbrella.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            answer.setVisibility(View.VISIBLE);
                            if("umbrella"==objects[i]){
                                answer.setText("Right Answer");
                                j++;

                            } else {
                                answer.setText("Wrong Answer");
                                answer.setTextColor(Color.RED);

                                answer.postDelayed(new Runnable() {
                                    public void run() {
                                        answer.setVisibility(View.INVISIBLE);
                                    }
                                }, 2000);

                            }

                        }
                    });





                }
                j = j+1;
                break;

           }


    }

2 个答案:

答案 0 :(得分:1)

你在j = j+1

之后的for循环中断了

答案 1 :(得分:0)

谢谢大家,我通过在OnClickListener()方法中迭代循环来解决它。感谢您的指导。