想在我的测验应用程序的CountDown Timer上添加一些功能。 [英] Want to add some features on CountDown Timer of my quiz application.

查看:73
本文介绍了想在我的测验应用程序的CountDown Timer上添加一些功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我做了一个测验申请,其中包括5个问题。我已经创建了一个显示测验结果的ResultActivity页面。

我为每个问题添加了一个20秒的countDown计时器。倒数计时器结束后,它会自动转到下一个问题。问题完成后,应移至ResultActivity页面以显示结果。



但我有一些问题需要解决:

1)如果用户没有选择任何选项并且计时器结束,那么它应该通过将错过的问题答案作为错误来转移到下一个问题。



2)我的倒数计时器没有得到在我完成最后一个问题之后就停止了。



3)如果我点击ResultActivity.java文件上的重启按钮,如何重置倒数计时器。 ...提前致谢.. :)





这是我的代码:

QuizActivity.java :



Hello,
I have made a quiz application which include 5 questions. I have made a ResultActivity page which displays result of the quiz.
I have added a countDown timer of 20 sec for each question. When the Countdown timer finishes it moves to the next question automatically. When the questions are finished it should move to the ResultActivity page to display result.

But i have some problem to solve:
1)if user does not select any option and timer finishes then it should move to next question by making the missed question answer as Wrong.

2) My countdown timer is not getting stop even after i have done with the last question.

3) And how to reset the countdown timer if i click the restart button which is on ResultActivity.java file....Thanks in advance..:)


Here is my code:
QuizActivity.java:

package com.example.triviality;
import java.util.LinkedHashMap;
import java.util.List;


import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends Activity {
	List<question> quesList;
	public static int score,correct,wrong,wronganswers;
	public boolean isCorrect;
	static int qid=0;
	int totalCount=5;
	Question currentQ;
	TextView txtQuestion;
	RadioGroup radioGroup1;
	RadioButton rda, rdb, rdc;
	Button butNext;
	TextView rt;
	boolean nextFlag =false;
	boolean isTimerFinished = false;
	static LinkedHashMap lhm = new LinkedHashMap();
	final MyCountDownTimer  countDownTimer = new MyCountDownTimer(20000 /*20 Sec*/, 1000);
	//final MyCountDownTimer timer = new MyCountDownTimer(20000,1000);
	public static String[] Correctanswers = new String[5];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quiz);
		DbHelper db=new DbHelper(this);
		quesList=db.getAllQuestions();
		currentQ=quesList.get(qid);
		txtQuestion=(TextView)findViewById(R.id.textView1);
		rda=(RadioButton)findViewById(R.id.radio0);
		rdb=(RadioButton)findViewById(R.id.radio1);
		rdc=(RadioButton)findViewById(R.id.radio2);
		butNext=(Button)findViewById(R.id.button1);
		//radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
		setQuestionView();
		// timer.start();
		rt  = (TextView)findViewById(R.id.rt);
        rt.setText("20");
       
        
        countDownTimer.start();

        butNext.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View v) {
                getNextQuestion();
                //Start The timer again
                countDownTimer.start();
            }
        });
		
	}
	
	private void setQuestionView()
	{
		txtQuestion.setText(currentQ.getQUESTION());
		rda.setText(currentQ.getOPTA());
		rdb.setText(currentQ.getOPTB());
		rdc.setText(currentQ.getOPTC());
	}
	
	public class MyCountDownTimer extends CountDownTimer {
	    public MyCountDownTimer(long startTime, long interval) {
	        super(startTime, interval);
	        
	    }

	    @Override
	    public void onFinish() {
	        Log.e("Times up","Times up");
	        getNextQuestion();
	        //Start The timer again
	        countDownTimer.start();
	    }

	    @Override
	    public void onTick(long millisUntilFinished) {
	    	rt.setText((millisUntilFinished/1000)+"");
	        Log.e("Second Gone","Another Second Gone"); 
	        Log.e("Time Remaining","seconds remaining: " + millisUntilFinished / 1000);
	    }
	}

	   void getNextQuestion(){
	            nextFlag = true;
	            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
	            //grp.clearCheck();
	            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
	            if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
	                qid++;
	                Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
	                grp.clearCheck();
	                //wronganswers=
	                if(currentQ.getANSWER().equals(answer.getText())){
	                    correct++;  
	                }else{
	                    lhm.put(currentQ.getQUESTION(),currentQ.getANSWER());
	                    wrong++;    
	                }if(qid<5){                  
	                    currentQ=quesList.get(qid);
	                    setQuestionView();
	                    
	                }else{
	                	
	                    score=correct;
	                    Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
	                    Bundle b = new Bundle();
	                    b.putInt("score", score); //Your score
	                    intent.putExtras(b); //Put your score to your next Intent
	                    startActivity(intent);  
	                }
	            }else{
	                Toast.makeText(getApplicationContext(), 
	                      "Please select atleast one Option",Toast.LENGTH_SHORT).show();
	            }
	   }
}




ResultActivity.java:

package com.example.triviality;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ResultActivity extends Activity {
	Button restart;
	Button check;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
	
		TextView t=(TextView)findViewById(R.id.textResult);
		TextView t1=(TextView)findViewById(R.id.textResult1);
		TextView t2=(TextView)findViewById(R.id.textResult2);
		restart=(Button)findViewById(R.id.restart);
		check=(Button)findViewById(R.id.check);
		
		StringBuffer sb=new StringBuffer();
		sb.append("Correct ans: "+QuizActivity.correct+"\n");
		StringBuffer sc=new StringBuffer();
		sc.append("Wrong ans : "+QuizActivity.wrong+"\n");
		StringBuffer sd=new StringBuffer();
		sd.append("Final Score : "+QuizActivity.score);
		t.setText(sb);
		t1.setText(sc);
		t2.setText(sd);
		QuizActivity.correct=0;
		QuizActivity.wrong=0;
		
		
		check.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				Intent in=new Intent(getApplicationContext(),CheckActivity.class);
				startActivity(in);	
			}
		});
		
         restart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getApplicationContext(),QuizActivity.class);
				startActivity(intent);
				QuizActivity.lhm.clear();
				QuizActivity.qid=0;
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_result, menu);
		return true;
	}
}

推荐答案

1)将一个布尔值传递给getNextQuestion方法,指示时间已到。

2)从getNextQuestion返回一个布尔值,表明还有更多问题。

3)不要从ResultActivity开始一个新活动,只需退出当前的活动并让它回到上一个。然后,在onResume中,重置计时器和游戏状态。



对于问题1和2,这些更新可能对您有所帮助(请注意,我实际上没有编译它,所以它可能有点坏了);





1) Pass a boolean to the getNextQuestion method indicating that time is up.
2) Return a boolean from getNextQuestion indicating if there are more questions.
3) Don't start a new activity from the ResultActivity, simply quit the current one and let it fall back to the previous one. Then, in onResume, reset the timer and game state.

For questions 1 and 2 these updates might help you (note that I haven't actually compiled this so it might be a bit broken);


 butNext.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {
         getNextQuestion(false);
         //Start The timer again
         countDownTimer.start();
     }
 });

 @Override
 public void onFinish() {
     Log.e("Times up","Times up");
     if (getNextQuestion(true)) {
         //Start The timer again
         countDownTimer.start();
 }
 }


boolean getNextQuestion(bool timeIsUp){
         nextFlag = true;
         RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
         //grp.clearCheck();
         RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
         if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
             qid++;
             Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
             grp.clearCheck();
             //wronganswers=
             if(!timeIsUp && currentQ.getANSWER().equals(answer.getText())){
                 correct++;
             }else{
                 lhm.put(currentQ.getQUESTION(),currentQ.getANSWER());
                 wrong++;
             }

     if(qid<5){
                 currentQ=quesList.get(qid);
                 setQuestionView();

             }else{

                 score=correct;
                 Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                 Bundle b = new Bundle();
                 b.putInt("score", score); //Your score
                 intent.putExtras(b); //Put your score to your next Intent
                 startActivity(intent);
                     return false;
             }
         }else{
             Toast.makeText(getApplicationContext(),
                   "Please select atleast one Option",Toast.LENGTH_SHORT).show();
         }

             return true;
}







希望这有帮助,

Fredrik




Hope this helps,
Fredrik


这篇关于想在我的测验应用程序的CountDown Timer上添加一些功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆