2015-09-19 216 views
0

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

这里是我的对话框:

@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(); 

      } 

     } 
    }; 

我已经尝试了第一个答案。 但是,当我删除statictimer它会在级别活动中发生错误。 这里是我的错误级别活动

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); 
    } 
} 
+0

你怎么显示TI我?你是否使用任何静态变量? –

+0

@rajanks,更新问题先生 –

+0

只是删除静态定时器,因为静态变量仅在创建对象 或 '壳体R.id.btn_score_retry第一时间实例: this.dialog.dismiss(); act.finish(); timer = 0; //你自己的计时器值 act.startActivity(new Intent(act,QuizActivity.class)); break;' –

回答

0

只需设置定时器到自己的价值,你在新的活动需要StartActivity

在竞猜活动

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: 
     } 
+0

我删除静态但它在关卡活动中发生错误。添加计时器= 0;但同样的先生。更新的问题 –

+0

它的工作,但任何级别现在有同样的时间,当我重试它@rajanks –

+0

你能给我一个例子吗?对不起,我是新手在java –