私はアンドロイドクイズに取り組んでいます。QuestionActivityクラスにsetQuestion()メソッドがあります。スコアを表示するTextViewを作成する question.xml があります。そのボタンの TextView にスコアを設定するように setQuestion にコードを書きたいと思います。setQuestion() メソッドのように、次の質問の回答 TextView が次のラウンドで更新されます。
public class GamePlay {
private int numRounds;
private int difficulty;
private int score;
private int round;
private List<Question> questions = new ArrayList<Question>();
/**
* @return the right
*/
public int getScore() {
return score;
}
/**
* @param right the right to set
*/
public void setScore(int score) {
this.score = score;
}
/**
* @return the round
*/
public int getRound() {
return round;
}
/**
* @param round the round to set
*/
public void setRound(int round) {
this.round = round;
}
/**
* @param difficulty the difficulty to set
*/
public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}
/**
* @return the difficulty
*/
public int getDifficulty() {
return difficulty;
}
/**
* @param questions the questions to set
*/
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
/**
* @param q the question to add
*/
public void addQuestions(Question q) {
this.questions.add(q);
}
/**
* @return the questions
*/
public List<Question> getQuestions() {
return questions;
}
public Question getNextQuestion(){
//get the question
Question next = questions.get(this.getRound());
//update the round number to the next round
this.setRound(this.getRound()+1);
return next;
}
/**
* method to increment the number of correct answers this game
*/
/**
* method to increment the number of incorrect answers this game
*/
public void incrementScore(){
score=score+100;
}
public void decrementScore()
{
score=score-50;
}
/**
* @param numRounds the numRounds to set
*/
public void setNumRounds(int numRounds) {
this.numRounds = numRounds;
}
/**
* @return the numRounds
*/
public int getNumRounds() {
return numRounds;
}
/**
* method that checks if the game is over
* @return boolean
*/
public boolean isGameOver(){
return (getRound() >= getNumRounds());
}
}
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
/**
* Configure current game and get question
*/
currentGame = ((ABCApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setOnClickListener(this);
Button nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setOnClickListener(this);
Button nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setOnClickListener(this);
Button nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion());
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
TextView score1=(TextView) findViewById(R.id.score);
setText score1=GamePlay.getScore();
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
if(!checkAnswer(arg0)) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()){
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer(View v) {
Button b=(Button) v;
String answer = b.getText().toString();
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
//Log.d("Questions", "Correct Answer!");
currentGame.incrementScore();
}
else{
//Log.d("Questions", "Incorrect Answer!");
currentGame.decrementScore();
}
return true;
}
question.xml
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/score_img"/>