Android 如何从倒计时计时器完成方法调用另一个意图?

Android 如何从倒计时计时器完成方法调用另一个意图?,android,android-intent,timer,scoring,Android,Android Intent,Timer,Scoring,我正在做安卓测验。在我的代码中,我有一个倒计时计时器,当时间结束时,我希望下一个问题出现并且分数减少(对于分数减少currentGame.decrementScore()方法)。如果我在计时器的finish()方法中添加以下代码。它可以正常工作,但我的退出按钮不能正常工作。当我点击该对话框时,按“是”会显示该对话框,它将保留在当前页面上。按3-4次后,退出当前活动并进入菜单页面。然后从菜单页面开始游戏,无需自动按下播放按钮 code for my class:- public class Que

我正在做安卓测验。在我的代码中,我有一个倒计时计时器,当时间结束时,我希望下一个问题出现并且分数减少(对于分数减少currentGame.decrementScore()方法)。如果我在计时器的finish()方法中添加以下代码。它可以正常工作,但我的退出按钮不能正常工作。当我点击该对话框时,按“是”会显示该对话框,它将保留在当前页面上。按3-4次后,退出当前活动并进入菜单页面。然后从菜单页面开始游戏,无需自动按下播放按钮

code for my class:-
public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
                /**
         * Configure current game and get question
         */
        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);

        new CountDownTimer(10000, 1000) {

            public void onTick(long millisUntilFinished) {
                TextView timers = (TextView) findViewById(R.id.timers);
                timers.setText("Time: " + millisUntilFinished / 1000);
            }

            public void onFinish() { 

                                   }
         }.start();
        }


    @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);
            startActivity(i);
            finish();
          }
        }
      }



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

            //Log.d("Questions", "Valid Checkbox selection made - check if correct");
            if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                b.setBackgroundResource(R.drawable.answercolor);
                //Log.d("Questions", "Correct Answer!");
                currentGame.incrementScore();
            }
            else{
                b.setBackgroundResource(R.drawable.answercolorr);
                //Log.d("Questions", "Incorrect Answer!");
                currentGame.decrementScore();
            }
            return true;
        }

}

创建一个新方法,比如说
processScreen()
,并在其中插入
onCreate
的数据,这样您就可以重复调用此方法,而无需再次调用相同的活动..(这是一种解决方法..)

因此,现在您的
onCreate
变为

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        processScreen();
}
现在,在按钮的
onClick
中再次调用
processScreen()
而不是调用
QuestionActivity
。换句话说,内容将被刷新


希望这有帮助。

按3-4次退出当前活动并进入菜单页面后,似乎有许多活动堆积在一起,请提供退出按钮的代码。,,如果您反复调用同一活动,请确保在调用下一步之前完成当前活动。。我补充道。您现在可以看到了。我不知道这是否有帮助,请尝试先调用
finish()
,然后调用您的活动…我已经尝试过该方法不起作用。好吧,您的问题是同一个活动堆积起来了,所以请解决问题,而不是再次调用同一个活动,尝试刷新其内容,换句话说,将
的内容放在onCreate()上
在一个新方法中,尝试调用该方法,而不是调用新活动……回答得很好,兄弟。但如何调用currentGame.decrementScore()方法来降低计时时的分数?现在的一个问题是,我的测验只进行了8、9或10轮,应用程序停止工作。。但我之前打了20发。在添加此代码之前,请先调试并找出为什么会出现这种行为,我认为这不会是一个主要问题,。。简单的逻辑问题在你的代码…好兄弟。但我想在计时时降低分数,并在我的代码currentGame.decrementScore()方法中降低分数。我要添加的地方和代码是什么?你能在这个链接上帮助我吗?
private void processScreen(){
        /**
         * Configure current game and get question
         */
        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();
}
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        processScreen();
}