[英]Android java - correct way to handle multiple buttons from main activity
[英]Selecting multiple correct answers from buttons
我的应用程序的游戏画面。
大家好,我目前正在开发测验应用程序游戏,其主要机制是每个问题都有一个或多个可能的正确答案。 到目前为止,我只能从给出的选择中选择一个正确的答案。
private Answer getAnswerFromCursor (Cursor cursor){
Answer answer = new Answer();
answer.setAnswerId(cursor.getInt(FIELD_ID_ID));
answer.setQuestionId(cursor.getInt(FIELD_ID_QUESTIONID));
answer.setText(cursor.getString(FIELD_ID_TEXT));
boolean correct = (cursor.getInt(FIELD_ID_CORRECT)==1);
answer.setCorrect(correct);
return answer;
}
布尔值更正用于在数据库中设置正确答案,其值true设置为1。
数据库中的正确答案设置为1。
private void displayQuestion(Question question) {
if (question != null) {
answers = answerData.getAnswersbyQuestionId(question.getQuestionId());
List<Integer> myAnswersIndexList = new ArrayList<Integer>();
for (int answerId : answers.keySet()) {
myAnswersIndexList.add(answerId);
}
Collections.shuffle(myAnswersIndexList);
answersFrame.removeAllViews();
questionText.setText(question.getText());
if(answers!=null){
answersButtonsList = new ArrayList<Button>();
Answer answer = null;
for(int answerId : myAnswersIndexList) {
Log.i("Captchabuster", "Adding question with Id " + answerId);
answer = answers.get(answerId);
Button answerButton = new Button(this);
answerButton.setId(answer.getAnswerId());
answerButton.setText(answer.getText());
//handle the button-clicker
answerButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//timer.cancel();
disableAnswerButtons();
answerButtonClickHandler(v);
}
});
answersButtonsList.add(answerButton);
answersFrame.addView(answerButton, answerButtonLayout);
}
}
}
}
如果选择了一个答案,则onClick方法将禁用剩下的答案,并调用answerButtonClickHandler。
else if(questionId == 8){
importAnswersData(db, questionId, recordData[4], false);
importAnswersData(db, questionId, recordData[6], true);
importAnswersData(db, questionId, recordData[5], false);
importAnswersData(db, questionId, recordData[7], true);
importAnswersData(db, questionId, recordData[8], true);
importAnswersData(db, questionId, recordData[9], true);
}
上面的代码段显示了如何将答案导入数据库。
public void answerButtonClickHandler(View v)
{
Answer answer = answers.get(v.getId());
if (answer != null) {
questionAnswered++;
if (answer.isCorrect()) {
correctlyAnswered++;
}
/* add template here
v.setBackgroundResource(R.drawable.answer_button_correct);
else{
v.setBackgroundResource(R.drawable.answer_button_wrong);
}*/
questionDescriptionText.setText(question.getDescription());
questionDescriptionText.setVisibility(View.VISIBLE);
if (nextQuestionIndex < questionsIndexList.size()) {
nextBtn.setVisibility(View.VISIBLE);
} else{
completeBtn.setVisibility(View.VISIBLE);
}
}
}
我想不出办法将用户选择的答案与插入数据库的答案进行比较。
我认为您可以使用CHeckBoxes而不是使用按钮,并在某个数组中获取二进制值并将其与数据库值进行比较,从UI角度来看,这可能会更容易且更干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.