2013-09-27 66 views
-1

如何將以下代碼與異步任務合併。我看到很多教程,並對代碼進行了更改,但無法完成。這段代碼是完全正常的,並且正常工作,但有人建議我使它成爲異步任務,這樣當我按下按鈕時,它需要一些時間才能啓動。所以請有人在其中添加異步任務代碼以使其工作正常。如何實現AsyncTask以延遲按鈕

代碼: -

public class QuestionActivity extends Activity implements OnClickListener{ 

private Question currentQ; 
private GamePlay currentGame; 
private CountDownTimer counterTimer; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.question); 
      processScreen(); 
    } 
      /** 
    * Configure current game and get question 
    */ 
    private void processScreen() 
    { 
    currentGame = ((CYKApplication)getApplication()).getCurrentGame(); 
    currentQ = currentGame.getNextQuestion(); 
    Button nextBtn1 = (Button) findViewById(R.id.answer1); 
    nextBtn1.setOnClickListener(this); 
    Button nextBtn2 = (Button) findViewById(R.id.answer2); 
    nextBtn2.setOnClickListener(this); 
    Button nextBtn3 = (Button) findViewById(R.id.answer3); 
    nextBtn3.setOnClickListener(this); 
    Button nextBtn4 = (Button) findViewById(R.id.answer4); 
    nextBtn4.setOnClickListener(this); 
    Button nextBtn5 = (Button) findViewById(R.id.answer5); 
    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) 
       System.exit(0); 
      currentGame.decrementScore(); 
      processScreen(); 
         } 

     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) { 
    //Log.d("Questions", "Moving to next question"); 
    if(arg0.getId()==R.id.answer5) 
    { 
    new AlertDialog.Builder(this).setMessage("Are you sure?").setCancelable(true).setPositiveButton("Yes", 
    new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, 
    int id) { 
      finish(); 
      } 
     }).setNegativeButton("No", null).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()); 
     Intent i = new Intent(this, EndgameActivity.class); 
     startActivity(i); 
     finish(); 
    } 
     else 
     { 
     Intent i = new Intent(this, QuestionActivity.class); 
        finish(); 
        startActivity(i); 
    } 
    } 
    } 



@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) { 

    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!"); 
      currentGame.incrementScore(); 
     } 
     else{ 
      b.setBackgroundResource(R.drawable.ansred); 
      //Log.d("Questions", "Incorrect Answer!"); 
      currentGame.decrementScore1(); 
     } 

     return true; 
    } 


} 

希望有人能幫助我。

由於提前

+0

你想延遲哪個代碼? –

+0

我想拖延按鈕(nextBtn1,nextBtn2,nextBtn3,nextBtn4)。 – UchihaSasuke

回答

1

使用Runnable

private static final int START_AFTER_SECONDS = 10; 
... 
Runnable mRunnable; 
Handler mHandler = new Handler(); 
mRunnable = new Runnable() { 
    @Override 
    public void run() { 
     //DELAYED CODE HERE 
    } 
}; 
mHandler.postDelayed(mRunnable, START_AFTER_SECONDS * 1000); 
+0

它是如何在我的代碼中鍛鍊的 – UchihaSasuke

+0

我不清楚你想要推遲什麼代碼。只需將它放在代碼所在的位置,然後將此代碼粘貼到'run()'方法內。 – sswierczek

1

的AsyncTask是不是這個工作的工具。試試這個

getHandler().postDelayed(
    new Runnable { 
      void run() 
      { 
      methodToCall(); 
      } 
     }, delayTimeInMilliseconds); 
+0

您可以將此代碼與我的代碼合併,我已經嘗試過但未成功實施它。 – UchihaSasuke

+0

我真的堅持使用runnable,Handler和asyncTask。 – UchihaSasuke