在逐个显示所有问题后,我希望再次显示第一条消息

时间:2016-07-12 18:29:20

标签: java android arrays

在Android上,在最后一个问题之后("是谁的Flavie?"),我想要第一个问题("谁是汤姆?")当我点击按钮

时再次显示
preventDefault()

1 个答案:

答案 0 :(得分:1)

复杂的方法

 final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
 final String answers[] = {"American", "Italian", "French"}

 // display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
public void onClick(View v) {
// make below line changes
ask_question.setText(question[(++CurrentQuestionIndex) % (questions.length-1)]);
// discuss question versus answer
EditText editText = (EditText) findViewById(R.id.tvReponseF);
if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
{
    alertMessageRight();
} else {
    alertMessageFalse();


  }
} 
});

简单的方法

final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
 final String answers[] = {"American", "Italian", "French"}

 // display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
public void onClick(View v) {

ask_question.setText(question[CurrentQuestionIndex]);

// discuss question versus answer
EditText editText = (EditText) findViewById(R.id.tvReponseF);
if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
{
    alertMessageRight();
} else {
    alertMessageFalse();


  }


 if(CurrentQuestionIndex++ == questions.length-1){
CurrentQuestionIndex =0;
      }
    } 

    });