活动重新启动但计时器未重新启动

时间:2015-09-19 05:42:21

标签: android

我做了一个简单的应用程序。我已尝试从按钮对话框重新启动活动,活动已重新启动,但时间不是。

这是我的对话:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_score_save:
            if(this.saveScoreCard()){
                this.dialog.dismiss();
                act.finish();
            }
            break;
        case R.id.btn_score_cancel: 
            this.dialog.dismiss();
            act.finish();
            break;
        case R.id.btn_score_retry:
            this.dialog.dismiss();
            act.finish();
            act.startActivity(new Intent(act,QuizActivity.class));
            break;
        case R.id.ing_btn_score_share:
            if(isValidated()){
                shareScore();
            }else{
                edt_name.setError(ERROR);
            }
            break;
        default:
            break;
        }
        //Closing the dialog

    }

这是我的时间码:

private TextView questionTextView,timer_text,score_text;
    private ImageButton option_1, option_2, option_3, option_4;
    private LinearLayout layout;

    private Handler handler=new Handler();
    public static int timer;
    private OnTimeCompleteListener timeComplete=(OnTimeCompleteListener)this;
    private Runnable timerThread=new Runnable() {


        @Override
        public void run() {

            if(timer>0){
                //Time is running 
                timer--;
                timer_text.setText("Time : "+timer);
                handler.postDelayed(this, 1000);
            }else{
                timeComplete.onTimeFinish();

            }

        }
    };

我试过第一个答案。 但是当我删除static中的timer时,它会在级别活动中出错。 这是我的错误级别活动

public class LevelActivity extends Activity implements OnClickListener {

    public static final String INTENT_KEY="Select Level";
    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level_activity_layout);

        SetHeader.handleHeader(findViewById(R.id.header_txt),"Select Level");
        findViewById(R.id.level_1).setOnClickListener(this);
        findViewById(R.id.level_2).setOnClickListener(this);
        findViewById(R.id.level_3).setOnClickListener(this);    
        findViewById(R.id.level_4).setOnClickListener(this);
        findViewById(R.id.level_5).setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
        Intent i=new Intent(this,QuizActivity.class);
        switch (v.getId()) {
        case R.id.level_1:  
            QuizActivity.timer = 60;
            QuizActivity.QUESTION_LIMIT = 10;

            break;
        case R.id.level_2:      
            QuizActivity.timer = 50;
            QuizActivity.QUESTION_LIMIT = 10;

            break;
        case R.id.level_3:          
            QuizActivity.timer = 30;
            QuizActivity.QUESTION_LIMIT = 10;
            break;
        case R.id.level_4:          
            QuizActivity.timer = 20;
            QuizActivity.QUESTION_LIMIT = 10;
            break;
        case R.id.level_5:          
            QuizActivity.timer = 10;
            QuizActivity.QUESTION_LIMIT = 10;

            break;
        default:
        }
        startActivity(i);




    }
}

我认为问题在于重试按钮,但我不知道什么是错的。它必须完美地工作。活动重新启动,但时间仍然没有重新启动,我的意思是它恢复了

public class QuizActivity extends BaseFragmentActivity implements OnClickListener,AnswerListener,
                                    OnTimeCompleteListener{

    private TextView questionTextView,timer_text,score_text;
    private ImageButton option_1, option_2, option_3, option_4;
    private LinearLayout layout;

    private Handler handler=new Handler();
    public static int timer;
    private OnTimeCompleteListener timeComplete=(OnTimeCompleteListener)this;
    private Runnable timerThread=new Runnable() {


        @Override
        public void run() {

            if(timer>0){
                //Time is running 
                timer--;
                timer_text.setText("Time : "+timer);
                handler.postDelayed(this, 1000);
            }else{
                timeComplete.onTimeFinish();

            }

        }
    };

    private int QUESTION_COUNTER=1;
    private int SCORE_COUNTER=0;
    public static int QUESTION_LIMIT;
    public static int LEVEL;
    private CurrentQuestion currentQuestion ;
    UtilityFile utility;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.quiz_activity_layout);


        layout=(LinearLayout)this.findViewById(R.id.subject_animate1);
        score_text=(TextView)this.findViewById(R.id.score);
        timer_text=(TextView)this.findViewById(R.id.time);
        score_text.setText("Score : "+"0/"+QUESTION_LIMIT);


        questionTextView = (TextView) this.findViewById(R.id.quiz_question);
        option_1 = (ImageButton) this.findViewById(R.id.quiz_level_option_1);
        option_2 = (ImageButton) this.findViewById(R.id.quiz_level_option_2);
        option_3 = (ImageButton) this.findViewById(R.id.quiz_level_option_3);
        option_4 = (ImageButton) this.findViewById(R.id.quiz_level_option_4);


        option_1.setOnClickListener(this);
        option_2.setOnClickListener(this);
        option_3.setOnClickListener(this);
        option_4.setOnClickListener(this);

        /**Utility for formating of the question*/
        utility=new UtilityFile();
        refreshQuestion();
        handler.postDelayed(timerThread, 10);
        Log.i("questionset", ""+QuizQuestionHandler.lev1.size());

    }


    public void refreshQuestion(){
        List<Integer> randomOptionOrdering=new ArrayList<Integer>();        
        currentQuestion = utility
                .setCurrentQuestionSet(QuizQuestionHandler.lev1);

        ImageButton buttons[]={option_1,option_2,option_3,option_4};
        int answerIndex=utility.randInt(0, 3);  
        Log.i("answertag",""+answerIndex);
        //Right Answer
        buttons[answerIndex].setImageDrawable(getResources().getDrawable(
                currentQuestion.getCurrentSet().getAnsImage()));
        buttons[answerIndex].setTag((Object)currentQuestion.getCurrentSet().getId());
        questionTextView.setText(""
                + currentQuestion.getCurrentSet().getQuestion());       
        //Options

        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_1().getAnsImage()));
        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_2().getAnsImage()));
        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_3().getAnsImage()));

    }

    public int randomOrder(List<Integer> rand,int currentAnswerIndex){
        while(true){
            int index = new UtilityFile().randInt(0,3);
            if(index!=currentAnswerIndex){
                if (!isInserted(rand, index)) {
                    rand.add(index);
                    Log.i("return",""+index);
                    return index;
                }
            }
        }   
    }
    public  boolean isInserted(List<Integer> inserted,int currentIndex){
        for(Integer inte:inserted){
            if(inte==currentIndex){
                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        disableOptionButton();
        AnswerFinder finder=null;
        switch (v.getId()) {        
        case R.id.quiz_level_option_1:
             finder=new AnswerFinder(option_1,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_2:
             finder=new AnswerFinder(option_2,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_3:
             finder=new AnswerFinder(option_3,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_4:
             finder=new AnswerFinder(option_4,currentQuestion , this);
             finder.getAnswer();
            break;      
        default:
            break;
        }       
    }
    private void disableOptionButton(){
    option_1.setClickable(false);
    option_2.setClickable(false);
    option_3.setClickable(false);
    option_4.setClickable(false);   
    }
    private void enableOptionButton(){
        option_1.setClickable(true);
        option_2.setClickable(true);
        option_3.setClickable(true);
        option_4.setClickable(true);
    }
    public void animateNext(){
        Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
        layout.startAnimation(anim);
        anim.setDuration(200);
        anim.setAnimationListener(new AnimationListener() {         
            @Override
            public void onAnimationStart(Animation animation) { 
                stopTimer();

            }           
            @Override
            public void onAnimationRepeat(Animation animation) {
            }           
            @Override
            public void onAnimationEnd(Animation animation) {
                /**Handling Animation Delays and Render Other Animations
                 * */
                Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.left_to_right);
                anim.setDuration(200);
                refreshQuestion();
                layout.startAnimation(anim);
                startTimer();
            }
        });
    }


    public void startTimer(){
        handler.postDelayed(timerThread, 100);
    }
    public void stopTimer(){
        handler.removeCallbacks(timerThread);
    }
    /**For getting the call of right and wrong answer*/
    @Override
    public void onAnswerClick(boolean answer) {     
        /**Check for the question overflow*/
        if(QUESTION_COUNTER<QUESTION_LIMIT){
            if(answer){
                SCORE_COUNTER++;
                AnswerHandler.rightAnswerHandler(score_text,this,SCORE_COUNTER,QUESTION_LIMIT);
            }else{
                //AnswerHandler.wrongAnswerHandler(this);
            }
            animateNext();
            QUESTION_COUNTER++;
        }else{
            /**Incrementing the final score*/           
            SCORE_COUNTER++;
            AnswerHandler.finalAnswerPlayer(this);
            this.gameCompleted();
        }
        enableOptionButton();
    }

    public void gameCompleted(){
        GameCompleteDialog dialog=new GameCompleteDialog(QuizActivity.this,SCORE_COUNTER);
        dialog.buildDialog();
        dialog.showDialog();            
        handler.removeCallbacks(timerThread);


    }
    public void onTimeFinish() {
        stopTimer();
        TimeCompleteDialog dialog=new TimeCompleteDialog(this);
        dialog.showDialog();

        AnswerHandler.vibrate(this);

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:0)

只需将Timer设置为您在StartActivity之前的New Activity中所需的值,

在测验活动中

public static int LEVEL=0,timer=0; //declare this in Quiz Activity

  case R.id.btn_score_retry:
                this.dialog.dismiss();
                act.finish();
     switch (LEVEL) {
        case 1:  
            QuizActivity.timer= 60;
            break;
        case 2:      
           QuizActivity.timer= 50;
            break;
        case R.id.level_3:          
           QuizActivity.timer=30;
            break;
        case R.id.level_4:          
            QuizActivity.timer= 20;
            break;
        case R.id.level_5:          
            QuizActivity.timer= 10;
            break;
        default:
        }
        act.startActivity(new Intent(act,QuizActivity.class));
      break;

在你的等级活动以及定时器设置等级

 @Override
    public void onClick(View v) {
        Intent i=new Intent(this,QuizActivity.class);
        switch (v.getId()) {
        case R.id.level_1:  
            QuizActivity.timer = 60;
            QuizActivity.QUESTION_LIMIT = 10;
            QuizActivity.LEVEL=1;
            break;
        case R.id.level_2:      
            QuizActivity.timer = 50;
            QuizActivity.QUESTION_LIMIT = 10;
            QuizActivity.LEVEL=2;
            break;
        case R.id.level_3:          
            QuizActivity.timer = 30;
            QuizActivity.QUESTION_LIMIT = 10;
            QuizActivity.LEVEL=3;
            break;
        case R.id.level_4:          
            QuizActivity.timer = 20;
            QuizActivity.QUESTION_LIMIT = 10;
            QuizActivity.LEVEL=4; 
            break;
        case R.id.level_5:          
            QuizActivity.timer = 10;
            QuizActivity.QUESTION_LIMIT = 10;
            QuizActivity.LEVEL=5;
            break;
        default:
        }