gpt4 book ai didi

java - for 循环的 onClickListener 不起作用

转载 作者:行者123 更新时间:2023-12-01 09:54:37 28 4
gpt4 key购买 nike

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

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;

}


}

最佳答案

您将在 j = j+1 之后脱离 for 循环

关于java - for 循环的 onClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37342604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com