2015-09-19 254 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 –