0

我正在做 android 测验,并希望在我的每个问答页面上都有计时器。我的测验中有菜单页面和开始游戏的播放按钮。我希望当我点击播放按钮时触发这个计时器。为此,我必须在代表我的菜单页面的问题 XML 中创建 TextView。以及代表我的第一个问题页面的 QuestionActivity 类中的实现。我也在发布 WelcomeActivity 类,尽管它在这个问题中没有任何作用。

播放按钮布局

<Button 
            android:text="Play" 
            android:id="@+id/playBtn"
            android:layout_width="80dip" 
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:paddingTop="5dip" 
            android:paddingBottom="5dip"
            android:textColor="#ffffff"
            android:background="@drawable/start_button" />

表示 Timer 的 TextView 的问题 XML

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/question"
    android:layout_centerHorizontal="true"
    android:background="@drawable/timer_bttn" 
    android:onClick="onClick"/>

QuestionActivity 我在哪里实现了计时器代码

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);
        /**
         * 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);
    }


    @Override
    public void onClick(View arg0) {
        //Log.d("Questions", "Moving to next question");
        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))
        {
        //Log.d("Questions", "Correct Answer!");
        currentGame.incrementScore();
        }
        else {
        //Log.d("Questions", "Incorrect Answer!");
        currentGame.decrementScore();
        }

        return true;
    }

    public void setTimer() {
        long finishTime = 5;
        CountDownTimer counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
            public void onFinish() {
                //code to execute when time finished
            }

            public void onTick(long millisUntilFinished) {
                int seconds = (int) (millisUntilFinished / 1000);
                int minutes = seconds / 60;
                seconds = seconds % 60;

                if (seconds < 10) {
                    txtTimer.setText("" + minutes + ":0" + seconds);
                } else {
                    txtTimer.setText("" + minutes + ":" + seconds);
                }
            }
        };
        counterTimer.start();
    }

}

欢迎活动

public class WelcomeActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);

    //////////////////////////////////////////////////////////////////////
    //////// GAME MENU  /////////////////////////////////////////////////
    Button playBtn = (Button) findViewById(R.id.playBtn);
    playBtn.setOnClickListener(this);
    Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(this);
    Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(this);
    Button exitBtn = (Button) findViewById(R.id.exitBtn);
    exitBtn.setOnClickListener(this);
}


/**
 * Listener for game menu
 */
@Override
public void onClick(View v) {
    Intent i;

    switch (v.getId()){
    case R.id.playBtn :
        //once logged in, load the main page
        //Log.d("LOGIN", "User has started the game");

        //Get Question set //
        List<Question> questions = getQuestionSetFromDb();

        //Initialise Game with retrieved question set ///
        GamePlay c = new GamePlay();
        c.setQuestions(questions);
        c.setNumRounds(getNumQuestions());
        ((CYKApplication)getApplication()).setCurrentGame(c);  

        //Start Game Now.. //
        i = new Intent(this, QuestionActivity.class);
        startActivityForResult(i, Constants.PLAYBUTTON);
        break;

    case R.id.rulesBtn :
        i = new Intent(this, RulesActivity.class);
        startActivityForResult(i, Constants.RULESBUTTON);
        break;

    case R.id.settingsBtn :
        i = new Intent(this, SettingsActivity.class);
        startActivityForResult(i, Constants.SETTINGSBUTTON);
        break;

    case R.id.exitBtn :
        finish();
        break;
    }

}
4

1 回答 1

0

正如Piyush Gupta所提到的,您应该setTimer()从.onResumeQuestionActivity

来自Android 开发者文档

(onResume is) called for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

在您的代码中,您还应该使counterTimer您在类成员中使用setTimer()不是局部变量;如果你不这样做,一旦调用完成,它就会超出范围setTimer(),你对它的访问就会丢失。

因此,您需要将以下内容添加到QuestionActivity

public class QuestionActivity extends Activity implements OnClickListener{

    // NEW: add counterTimer as a member
    private CountDownTimer counterTimer;

    // NEW: implement onResume
    @Override
    public void onResume() {
        setTimer();
        super.onResume();
    }

    // CHANGE: setTimer should be changed as follows
    public void setTimer() {
        long finishTime = 5;
        // NOTE: use the member, instead of a local
        counterTimer = new CountDownTimer(finishTime * 1000, 1000) {
            public void onFinish() {
                //code to execute when time finished
            }

            public void onTick(long millisUntilFinished) {
                int seconds = (int) (millisUntilFinished / 1000);
                int minutes = seconds / 60;
                seconds = seconds % 60;

                if (seconds < 10) {
                    txtTimer.setText("" + minutes + ":0" + seconds);
                } else {
                    txtTimer.setText("" + minutes + ":" + seconds);
                }
            }
        };
        counterTimer.start();
    }
}

上面的示例使用您现在的代码,但我建议您onCreate使用类成员创建计时器来存储它(即当前在 setTimer() 中的所有内容,除了counterTimer.start();调用。然后只需使用counterTimer.start();in onResume。并且可能添加一个counterTimer.cancel()调用,onPause以便当活动失去焦点时计时器结束。

于 2013-09-05T22:42:26.297 回答