从按钮中选择多个正确答案
Selecting multiple correct answers from buttons
我的应用程序的游戏画面。
大家好,我目前正在开发我的测验应用游戏,其主要机制是每个问题可能有一个或多个可能的正确答案。到目前为止,我只能 select 给出选项中的一个正确答案。
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;
}
boolean correct 用于设置数据库中的正确答案,其值为 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);
}
}
}
}
如果选项中的答案已被 selected 并调用 answerButtonClickHandler,则 onClick 方法会禁用待回答的答案。
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);
}
}
}
我想不出一种方法来比较用户select输入的答案与我插入数据库的答案。
我认为您可以使用 CHeckBoxes 而不是使用按钮并在某个数组中获取二进制值并将其与您的数据库值进行比较,我认为从 UI 的角度来看这可能更容易和更清晰。
我的应用程序的游戏画面。
大家好,我目前正在开发我的测验应用游戏,其主要机制是每个问题可能有一个或多个可能的正确答案。到目前为止,我只能 select 给出选项中的一个正确答案。
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;
}
boolean correct 用于设置数据库中的正确答案,其值为 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);
}
}
}
}
如果选项中的答案已被 selected 并调用 answerButtonClickHandler,则 onClick 方法会禁用待回答的答案。
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);
}
}
}
我想不出一种方法来比较用户select输入的答案与我插入数据库的答案。
我认为您可以使用 CHeckBoxes 而不是使用按钮并在某个数组中获取二进制值并将其与您的数据库值进行比较,我认为从 UI 的角度来看这可能更容易和更清晰。