如何在Android测验中随机生成的数组中找到正确的答案?

时间:2013-08-07 21:56:13

标签: android

我想知道如果它按下正确的答案,如果点击该按钮是绿色的。我不知道哪个按钮保持正确答案,因为它是随机生成的。此外,我已经输入了代码,当单击按钮时,它会暂停一段时间,然后移动到一个新的问题,所以我希望按钮在活动持有的时候变为绿色,所以当一个新的时候问题产生了,它又回过头来。

public class Glavno extends Activity implements OnClickListener {

  final Handler handler = new Handler();

  int score  = 0;
  int counter = 0;
  boolean ajme = true;

  TextView textView1, textView2, textView3, countdown;
  Button btn1, btn2, btn3, btn4;

  ArrayList<Question> qsts = new ArrayList<Question>();
  List<Integer> generated = new ArrayList<Integer>();

  ArrayList<String> allAnswers = new ArrayList<String>();

  Random rng = new Random();
  Question nextQuestion;

  Question qjedan = new Question(
    "Q1",

    "Correct answer - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1"
  );
  Question q2 = new Question(
    "Q2",

    "Correct answer - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2"
  );
  Question q3 = new Question(
    "Q3",

    "Correct answer - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3"
  );


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.pitanja);

    new CountDownTimer(20000, 1000) {

     public void onTick(long millisUntilFinished) {
       textView4.setText("Seconds remaining: " + millisUntilFinished / 1000);
     }

   public void onFinish() {
     generateQuestion();
     textView2.setText("VRIJEME!");
     textView2.setTextColor(Color.RED);

 }
  }.start();

qsts.add(qjedan);           
qsts.add(q2);
qsts.add(q3);

textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);

textView3.setText("Rezultat: " + score);

btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);

generateQuestion();

}

 public void generateQuestion(){

while(ajme = true){

    int nxt = rng.nextInt(3);

    if (!generated.contains(nxt)){

        generated.add(nxt);

        nextQuestion = qsts.get(nxt);

        textView1.setText(nextQuestion.questionText);

        allAnswers.add(nextQuestion.correctAnswerText);
        allAnswers.add(nextQuestion.wrongAnswer1);
        allAnswers.add(nextQuestion.wrongAnswer2);
        allAnswers.add(nextQuestion.wrongAnswer3);

        Collections.shuffle(allAnswers);

        btn1.setText(allAnswers.get(0));
        btn2.setText(allAnswers.get(1));
        btn3.setText(allAnswers.get(2));
        btn4.setText(allAnswers.get(3));

        break;
    }
}
}


@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();

if (counter == 3) {

    Intent theIntent = new Intent(this, Score.class);
    theIntent.putExtra("somename", score);  
    startActivity(theIntent);

    finish();   // Added this method call
}

else if(buttonText.equals(nextQuestion.correctAnswerText)) { 

    counter++;

    textView3.setText("Rezultat: " + (score += 10));

    handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    allAnswers.clear();
                    generateQuestion();

                }
              }, 1000);

    return;
} 

else{

    counter++;

    textView2.setTextColor(Color.RED);

    handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    allAnswers.clear();
                    generateQuestion();

                }
              }, 1000);

    return; 
}

}   

1 个答案:

答案 0 :(得分:0)

使用您投射到View的{​​{1}}。您必须将声明更改为:

Button

现在,如果按钮保持正确答案,请将其背景设置为绿色。在处理程序中,将背景还原为原始颜色:

final Button b = (Button)v;