【问题标题】:how to create timer thats start automatically?如何创建自动启动的计时器?
【发布时间】:2013-09-05 22:35:44
【问题描述】:

我正在我的应用程序中进行 android 测验我有 QuestionActivity 类。在哪个 setQuestions() 方法中,来自数据库的文本在按钮 textview 中分配,并且第一次将分数设置为零。在下一个问题的下一个文本中分配,并且无论问题是错误还是正确,分数也会更新。我想要一个自动计时器,当我们进入这个活动时它就会启动。并且在下一轮它从某个时间再次开始并继续减少。我知道为此我必须在与此调用相关的问题 xml 中创建 textview。但我不知道在哪里以及如何在这个类中编写代码。计时器时间 30 秒。我正在发布此类的代码。请有人帮助我。

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


    @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;
        }

}

【问题讨论】:

    标签: android timer automatic-updates


    【解决方案1】:

    我想要一个自动计时器,当我们进入这个时启动 活动。并且在下一轮它从某个时间再次开始并继续 减少。我知道为此我必须在问题 xml 中创建 textview 与此通话有关。但我不知道在哪里以及如何编写代码 这节课。计时器时间 30 秒。

    您可以使用 CountDownTimer 实现此目的

    new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
      }.start();
    

    欲了解更多信息refer here

    【讨论】:

    • 我没有得到你。我认为仅此代码不足以像我在游戏中想要的那样计时。你能给我其他的代码解决方案吗?
    • 为什么不够?计时器将在活动中起作用...您的问题部分将被刷新...我建议您是因为我做了类似的问答测验应用程序..
    • 好的。但是当我分配我在问题 xml 中创建的 textview 代替 mTextField.setText("") 时。它的显示错误。
    • 我解决了错误。它的工作。你解决了我的问题。兄弟。如果我需要任何其他帮助,我会发短信给你,因为你参加了测验。
    • @JohnR 很高兴为您提供帮助。顺便说一句,我不是唯一一个。您可以随时在此站点上发布新问题,有很多像我这样的人可以指导您正确的方向。 . :)
    【解决方案2】:

    对于计时器,有一个计时器类,我用它每 300 毫秒从手机发送一次数据。但是你需要一个计数器而不是一个计时器,因为你使用一个线程处理程序和一个从 counter=30 开始的整数,在处理程序中你将它减少一个计数器 - 并且处理程序是 postat 1sec(1000ms)

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2021-12-17
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多