アプリケーションにカウントダウンタイマーを実装しました。正常に動作しています。今、ダイアログ ボックスのアクセス許可 (はいまたはいいえ) を使用してアプリに終了ボタンを実装しました。ユーザーが [はい] を押してから終了し、[いいえ] を押してゲームに戻ります。終了ボタンをクリックするとカウントダウンタイマーを一時停止し、ダイアログボックスのボタンなしで再開します。
これが私のコードです:-
public class QuestionActivity extends Activity implements OnClickListener{
private Question currentQ;
private GamePlay currentGame;
public CountDownTimer counterTimer;
private Button nextBtn1, nextBtn2, nextBtn3, nextBtn4, nextBtn5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
processScreen();
}
/**
* Configure current game and get question
*/
private void processScreen()
{
currentGame = ((Application)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
nextBtn1 = (Button) findViewById(R.id.answer1);
nextBtn1.setEnabled(true);
nextBtn1.setOnClickListener(this);
nextBtn2 = (Button) findViewById(R.id.answer2);
nextBtn2.setEnabled(true);
nextBtn2.setOnClickListener(this);
nextBtn3 = (Button) findViewById(R.id.answer3);
nextBtn3.setEnabled(true);
nextBtn3.setOnClickListener(this);
nextBtn4 = (Button) findViewById(R.id.answer4);
nextBtn4.setEnabled(true);
nextBtn4.setOnClickListener(this);
nextBtn5 = (Button) findViewById(R.id.answer5);
nextBtn5.setEnabled(true);
nextBtn5.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)));
int score = currentGame.getScore();
String scr = String.valueOf(score);
TextView score1 = (TextView) findViewById(R.id.score);
score1.setText(scr);
counterTimer = new CountDownTimer(15000, 1000) {
public void onFinish() {
if(currentGame.getRound()==20)
{
nextBtn1.setEnabled(false);
nextBtn2.setEnabled(false);
nextBtn3.setEnabled(false);
nextBtn4.setEnabled(false);
nextBtn5.setEnabled(false);
final Handler handle = new Handler();
Toast.makeText(QuestionActivity.this, "Time's Up", Toast.LENGTH_SHORT).show();
Runnable delay = new Runnable() {
public void run() {
System.exit(0);
}
};
handle.postDelayed(delay,3000);
}
else if(currentGame.getRound()==0)
{
currentGame.decrementScore();
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
processScreen();
}
};
handle.postDelayed(delay,3000);
}
else if(currentGame.getRound()==19)
{
nextBtn1.setEnabled(false);
nextBtn2.setEnabled(false);
nextBtn3.setEnabled(false);
nextBtn4.setEnabled(false);
nextBtn5.setEnabled(false);
currentGame.decrementScore();
final Handler handle = new Handler();
Toast.makeText(QuestionActivity.this, "Time's Up", Toast.LENGTH_SHORT).show();
Runnable delay = new Runnable() {
public void run() {
processScreen();
}
};
handle.postDelayed(delay,3000);
}
}
public void onTick(long millisUntilFinished) {
TextView time = (TextView) findViewById(R.id.timers);
time.setText( ""+millisUntilFinished/1000);
}
};
counterTimer.start();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onClick(View arg0)
{
nextBtn1.setEnabled(false);
nextBtn2.setEnabled(false);
nextBtn3.setEnabled(false);
nextBtn4.setEnabled(false);
nextBtn5.setEnabled(false);
arg0.setEnabled(true);
//Log.d("Questions", "Moving to next question");
if(arg0.getId()==R.id.answer5)
{
Button yes, no;
final Dialog dialog = new Dialog(this, R.style.FullHeightDialog); //this is a reference to the style above
dialog.setContentView(R.layout.dialog1); //I saved the xml file above as yesnomessage.xml
dialog.setCancelable(true);
//to set the message
TextView message =(TextView) dialog.findViewById(R.id.tvmessagedialogtext);
message.setText("Are you sure you want to Exit?");
yes = (Button) dialog.findViewById(R.id.bmessageDialogYes);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
counterTimer.cancel();
finish();
startActivity(new Intent(QuestionActivity.this, SplashActivity.class));
}
});
no = (Button) dialog.findViewById(R.id.bmessageDialogNo);
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dialog.dismiss();
nextBtn1.setEnabled(true);
nextBtn2.setEnabled(true);
nextBtn3.setEnabled(true);
nextBtn4.setEnabled(true);
}
});
dialog.show();
}
else
{
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());
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(QuestionActivity.this, EndgameActivity.class));
finish();
}
};
handle.postDelayed(delay,2000);
}
else
{
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(QuestionActivity.this, QuestionActivity.class));
finish();
}
};
handle.postDelayed(delay,2000);
}
}
}
@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) {
final Button b = (Button) v;
String answer = b.getText().toString();
counterTimer.cancel();
b.setBackgroundResource(R.drawable.ans);
b.setEnabled(false);
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
b.setBackgroundResource(R.drawable.ansgreen);
//Log.d("Questions", "Correct Answer!");
Toast.makeText(QuestionActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
currentGame.incrementScore();
}
else{
b.setBackgroundResource(R.drawable.ansred);
//Log.d("Questions", "Incorrect Answer!");
Toast.makeText(QuestionActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
currentGame.decrementScore1();
}
return true;
}
public void show()
{
counterTimer.cancel();
}
}