我做了一個簡單的問答遊戲的android,現在只有10個問題和40個答案。 (每個問題4個答案)有時,當我點擊一個按鈕時,它一次給我一個以上的正確答案!任何想法有什麼錯此代碼:OnClick監聽器行爲奇怪(1次點擊 - 多次呼叫它?)
public class ETBetaActivity extends Activity implements View.OnClickListener {
Button answer_1,
answer_2,answer_3,
answer_4,main;
TextView q_textview,
tip;
private String a1,a2,a3,a4 = "";
private int i1 = 0;
public static int correct = 0;
private boolean alive = true;
MediaPlayer button_click;
private String[] questions =
{"Q1",
"Q2",
"Q3",
"Q4",
"Q5", //5
"Q6",
"Q7",
"Q8",
"Q9",
"Q10" //10
};
public static int question_amount = 10;
private String[] answers_correct =
{"Correct answer - 1",
"Correct answer - 2",
"Correct answer - 3",
"Correct answer - 4",
"Correct answer - 5",
"Correct answer - 6",
"Correct answer - 7",
"Correct answer - 8",
"Correct answer - 9",
"Correct answer - 10"
};
private String[][] answers_wrong =
{ {"Q1-1", "Q1-2" , "Q1-3"},
{"Q2-1", "Q2-2" , "Q2-3"},
{"Q3-1", "Q3-2" , "Q3-3"},
{"Q4-1", "Q4-2" , "Q4-3"},
{"Q5-1", "Q5-2" , "Q5-3"},
{"Q6-1", "Q6-2" , "Q6-3"},
{"Q7-1", "Q7-2" , "Q7-3"},
{"Q8-1", "Q8-2" , "Q8-3"},
{"Q9-1", "Q9-2" , "Q9-3"},
{"Q10-1", "Q10-2" , "Q10-3"}
};
List<String> question_list = new ArrayList<String>();
List<String> answer_list_correct = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getData();
Game(i1);
}
@Override
public void onClick(View view) {
if (alive == false) {
// startActivity(new Intent("com.aleksei.etb.END"));
return;
}
button_click = MediaPlayer.create(this, R.raw.button_click);
button_click.start();
switch(view.getId()){
case R.id.button5: //main
break;
case R.id.button1: //answer_1
if(isCorrect(1))
correct++;
break;
case R.id.button2: //answer_2
if(isCorrect(2))
correct++;
break;
case R.id.button3: //answer_3
if(isCorrect(3))
correct++;
break;
case R.id.button4: //answer_3
if(isCorrect(4))
correct++;
break;
default:
break;
}
Game(i1);
tip.setText("Correct answers: "+correct);
}
public static int getResults(){
int value = (int) Math.floor((correct*5)/question_amount);
if(value <= 0)
return 1;
else
return value;
}
private boolean isCorrect(int button){
for (int i = 0; i < answers_correct.length; i++){
if(button == 1 && a1 == answers_correct[i]
|| button == 2 && a2 == answers_correct[i]
|| button == 3 && a3 == answers_correct[i]
|| button == 4 && a4 == answers_correct[i])
return true;
}
return false;
}
private void Game(int q){
if(i1 == question_amount) { //no more questions
startActivity(new Intent("com.aleksei.etb.END"));
alive = false;
return;
}
try {
main.setText("Dunno");
String answer_list[] = {
answers_correct[q], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]
};
Collections.shuffle(Arrays.asList(answer_list));
answer_1.setText(answer_list[0]);
answer_2.setText(answer_list[1]);
answer_3.setText(answer_list[2]);
answer_4.setText(answer_list[3]);
a1 = answer_list[0];
a2 = answer_list[1];
a3 = answer_list[2];
a4 = answer_list[3];
q_textview.setText(questions[q]);
} catch (Exception ex){}
i1++;
}
private void getData(){
//Getting the data
main = (Button) findViewById(R.id.button5);
answer_1 = (Button) findViewById(R.id.button1);
answer_2 = (Button) findViewById(R.id.button2);
answer_3 = (Button) findViewById(R.id.button3);
answer_4 = (Button) findViewById(R.id.button4);
q_textview = (TextView) findViewById(R.id.question);
tip = (TextView) findViewById(R.id.answ1);
//Making the buttons, actually work
main.setOnClickListener(this);
answer_1.setOnClickListener(this);
answer_2.setOnClickListener(this);
answer_3.setOnClickListener(this);
answer_4.setOnClickListener(this);
//Resets the text
//Note to self: Replace with another ContectView
main.setText("Begin!");
answer_4.setText("");
answer_3.setText("");
answer_2.setText("");
answer_1.setText("");
tip.setText("");
}
}
我甚至想是這樣
private boolean getAnswer = false;
private String correctAnswer;
private boolean isCorrect(int button){
if(getAnswer == true) {
if(button == 1 && a1 == correctAnswer
|| button == 2 && a2 == correctAnswer
|| button == 3 && a3 == correctAnswer
|| button == 4 && a4 == correctAnswer)
return true;
}
return false;
}
遊戲內(INT):
getAnswer = false;
correctAnswer = answers_correct[q];
沒有幫助。
我希望我能讓自己清楚,如果不是,請問。
謝謝。
編輯: 在onCreate方法
answer_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isCorrect(1))
correct++;
Game(i1);
tip.setText("Correct answers "+correct);
}
});
answer_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isCorrect(2))
correct++;
Game(i1);
tip.setText("Correct answers "+correct);
}
});
answer_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isCorrect(3))
correct++;
Game(i1);
tip.setText("Correct answers "+correct);
}
});
answer_4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isCorrect(4))
correct++;
Game(i1);
tip.setText("Correct answers "+correct);
}
});
沒有幫助使用這些嘗試。
[爲什麼它會多次調用方法'isCorrect'(onClick),如果我只按一次按鈕?](http://stackoverflow.com/questions/8169960/why-does-it-call-方法 - 正確 - 多次 - 點擊 - 如果我按了按鈕) - 請不要轉貼同一個問題 – BoltClock