测验应用程序,改变错误的答案选择错误和正确的答案的颜色,android
我正在一个android测验应用程序,我需要改变按钮的颜色时,选择了错误的答案。如果他们选择的按钮是错误的,我将它设置为已经改变颜色或者如果它们选择的按钮是正确的,则为。那么,如果选择了错误的答案,最好的方法是用正确的答案改变按钮的颜色?如果这是有道理的。基本上我需要有2个按钮,当你选择错误的答案时颜色会改变。红色代表他们选择错误的答案,绿色代表正确的答案,他们显然没有选择。答案也在洗牌,所以不知道如何判断哪一个是正确的。测验应用程序,改变错误的答案选择错误和正确的答案的颜色,android
也尝试拼写出来,给我的例子,因为我只是让我的脚在所有这些湿!它运行在一个片段中,如果任何人有任何关于如何简化任何东西或修复错误代码的指示,请随时告诉我,因为我刚开始学习,需要我可以获得的所有帮助。
这里是我的onActivityCreated
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
timesUp = (TextView)getActivity().findViewById(R.id.times_up);
timesUp.setVisibility(View.INVISIBLE);
Bundle c;
c = getArguments();
String setCategory = c.getString("category");
QuizHelper db = new QuizHelper(this.getActivity()); // my question bank class
quesList = db.getAllQuestions(setCategory); // this will fetch all quetonall questions
currentQ = quesList.get(qid);
txtQuestion = (TextView)getActivity().findViewById(R.id.txtQuestion);
// the textview in which the question will be displayed
// the buttons,
// the idea is to set the text of the buttons with the options from question bank
button1 = (Button)getActivity().findViewById(R.id.button1);
button2 = (Button)getActivity().findViewById(R.id.button2);
button3 = (Button)getActivity().findViewById(R.id.button3);
button4 = (Button)getActivity().findViewById(R.id.button4);
//set buttons white
button1.setBackgroundColor(Color.WHITE);
button2.setBackgroundColor(Color.WHITE);
button3.setBackgroundColor(Color.WHITE);
button4.setBackgroundColor(Color.WHITE);
// the textview in which score will be displayed
scored = (TextView)getActivity().findViewById(R.id.score);
scored.setText(getResources().getString(R.string.current_score, score));
// the timer
times = (TextView)getActivity().findViewById(R.id.timers);
// method which will set the things up for our game
allAnswers.clear();
setQuestionView();
times.setText(getResources().getString(R.string.start_time));
// Start timer
timer.start();
// button click listeners
// passing the button text to other method
// to check whether the answer is correct or not
// same for all three buttons
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button2);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button3);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button4);
}
});
}
这里是我的的getAnswer()(缩短一点)
public void getAnswer(TextView AnswerString) {
String option = AnswerString.getText().toString();
//If timer is running stop and restart
if(timer != null) {
timer.cancel(); // Stop Timer
timer.start(); // Start timer
}
if (currentQ.getANSWER().equals(option)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
score++;
scored.setText(getResources().getString(R.string.current_score, score));
AnswerString.setBackgroundColor(Color.GREEN);
} else {
//If answer is wrong change button colors
AnswerString.setBackgroundColor(Color.RED);
//WrongAnswer.setBackgroundColor(Color.GREEN);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Load ResultFragment()...
}
}, 1000);
return;
}
if (qid < 25) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// if questions are not over then do this
currentQ = quesList.get(qid);
allAnswers.clear();
//set all buttons white for next question
button1.setBackgroundColor(Color.WHITE);
button2.setBackgroundColor(Color.WHITE);
button3.setBackgroundColor(Color.WHITE);
button4.setBackgroundColor(Color.WHITE);
setQuestionView();
}
}, 1000);
} else {
// if "game over" (qid>25) do this
//Load ResultFragment()...
}
最后我questionView()
private void setQuestionView() {
allAnswers.add(currentQ.getOPTA());
allAnswers.add(currentQ.getOPTB());
allAnswers.add(currentQ.getOPTC());
allAnswers.add(currentQ.getOPTD());
Collections.shuffle(allAnswers);
txtQuestion.setText(currentQ.getQUESTION());
button1.setText(allAnswers.get(0));
button2.setText(allAnswers.get(1));
button3.setText(allAnswers.get(2));
button4.setText(allAnswers.get(3));
qid++;
}
感谢您的帮助!
编辑
你可以得到按钮上的文字:从getAnswer(Button selectedButton)
错误的答案场
获取答案
// for currentQ.getANSWER().equals(option)
String option = selectedButton.getText();
之前编辑
调用此
//match DB answer to selected answer, turn it green if it is correct
if(button1.getText().equals(answerText)){
button1.setBackgroundColor(Color.GREEN);
} else if(button2.getText().equals(answerText)){
button2.setBackgroundColor(Color.GREEN);
} else if(button3.getText().equals(answerText)){
button3.setBackgroundColor(Color.GREEN);
} else if(button4.getText().equals(answerText)){
button4.setBackgroundColor(Color.GREEN);
}
选项2:
// match DB answer to selected answer, turn it green if it is correct
ArrayList buttonList = new ArrayList();
buttonList.add(button1);
buttonList.add(button2);
buttonList.add(button3);
buttonList.add(button4);
for(Button button : buttonList){
if(button.getText().equals(answerText)){
button.setBackgroundColor(Color.GREEN);
break;
}
}
注:从您的数据库,并与您的所有按钮的getText()
// because you found out that selected answer/button was wrong
selectedButton.setBackgroundColor(Color.RED);
// take real answer from DB and store in String, probably you've already done this
String answerText = "take-answer-from-database";
选项1相匹配,如果您getAnswer(Button)
是在不同的课程中,只需制作你的按钮全球和public
,那么,他们的类对象来访问他们
让我知道如果你需要更多的帮助
这很好用!我使用了选项1,并将所有内容放在我的'AnswerString.setBackgroundColor(Color.RED);'下,并且使用'String answerText = currentQ.getANSWER();'取出正确的答案;如果有其他人想知道! – Xenocide122
你怎么能叫'的getAnswer(按钮)'时,它仅在'的getAnswer接受'TextView'( TextView)' – rupinderjeet
因为在'getAnswer(TextView的AnswerString)'我使用'字符串选项= AnswerString.getText()。toString();'将其更改为一个字符串,然后我可以使用'option'来比较'currentQ.getANSWER()。equals(option)' – Xenocide122