Android测验计时器问题

时间:2013-05-14 10:19:02

标签: android countdowntimer

  

实施1问题

     

怀疑1 :我使用进度条尝试了计时器。它就像每个问题说5秒。但是,进度条会显示倒计时值。我的问题是在时间结束后(值达到0),我默认不会进入下一个问题..

     

实施2问题

     

怀疑2 :另外,如何使用从第一个问题开始的进度条设置倒数计时器,并持续降低值直到达到指定的限制。就像说我想要我的测验中的所有20个问题都要在120分钟内得到解答。   所以进度条应为120,119 .... 1.达到0后,我应该自动重定向到结果页面。

QuestionActivity.java

package works.really.good;

import java.sql.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import works.really.good.quiz.GamePlay;
import works.really.good.quiz.Question;
import works.really.good.util.Utility;

public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;
    ProgressBar mProgressBar;
    CountDownTimer mCountDownTimer;
    TextView tv;
    int i=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        currentGame = ((MyApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn = (Button) findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(this);
        setQuestions();

        tv = (TextView) findViewById(R.id.tv_counter);
        mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
        mProgressBar.setProgress(i);
           mCountDownTimer=new CountDownTimer(5000,1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
                    i++;
                    String time = (String) DateFormat.format("ss", new Date(millisUntilFinished));
                    tv.setText(time);
                    mProgressBar.setProgress(i);

                }

                @Override
                public void onFinish() {
                //Do what you want 
                    i++;
                    mProgressBar.setProgress(i);
                }
            };
            mCountDownTimer.start();

    }


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


    @Override
    public void onClick(View arg0) {

        /**
         * validate a checkbox has been selected
         */
        if (!checkAnswer()) return;


        /**
         * check if end of game
         */
        if (currentGame.isGameOver()){
            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);
    }


    private boolean checkAnswer() {
        String answer = getSelectedAnswer();
        if (answer==null){
            return false;
        }
        else {
                if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                    currentGame.incrementRightAnswers();
            }
            else{
                    currentGame.incrementWrongAnswers();
            }
            return true;
        }
    }


    private String getSelectedAnswer() {
        RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
        RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
        RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
        RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
        if (c1.isChecked())
        {
            if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
            {
                c1.setTextColor(Color.GREEN);
            }
            else
            {
                c1.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }

            }
            return c1.getText().toString();
        }
        if (c2.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
            {
                c2.setTextColor(Color.GREEN);
            }   
            else
            {
                c2.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }
            }
            return c2.getText().toString();
        }
        if (c3.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
            {
                c3.setTextColor(Color.GREEN);
            }
            else
            {
                c3.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
                {
                    c4.setTextColor(Color.GREEN);
                }
            }
            return c3.getText().toString();
        }
        if (c4.isChecked())
        {
            if(currentQ.getAnswer().equalsIgnoreCase(c4.getText().toString()))
            {
                c4.setTextColor(Color.GREEN);
            }
            else
            {
                c4.setTextColor(Color.RED);
                if (currentQ.getAnswer().equalsIgnoreCase(c1.getText().toString()))
                {
                    c1.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c2.getText().toString()))
                {
                    c2.setTextColor(Color.GREEN);
                }
                else if (currentQ.getAnswer().equalsIgnoreCase(c3.getText().toString()))
                {
                    c3.setTextColor(Color.GREEN);
                }
            }
            return c4.getText().toString();
        }

        return null;
    }

}

Question.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_horizontal"
    android:background="@drawable/background">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="15dip" android:paddingBottom="15dip"
        android:gravity="center_horizontal">

        <ImageView android:id="@+id/logo" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:src="@drawable/logo2"
            android:contentDescription="Quiz_Logo" />

    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip"
        android:gravity="center_horizontal" >

        <RadioGroup android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:id="@+id/group1">

            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textColor="#ffffff"
                android:textStyle="bold" android:id="@+id/question"/>

            <RadioButton android:checked="false" android:id="@+id/answer1" />

            <RadioButton android:checked="false" android:id="@+id/answer2" />

            <RadioButton android:checked="false" android:id="@+id/answer3" />

            <RadioButton android:checked="false" android:id="@+id/answer4" />

        </RadioGroup>

    </LinearLayout>



    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip"
        android:gravity="center_horizontal">

        <Button android:text="Next" android:id="@+id/nextBtn"
            android:layout_width="80dip" 
            android:layout_height="wrap_content" android:background="@drawable/button_selector"
            android:paddingTop="5dip" android:paddingBottom="5dip"
            android:textColor="#ffffff" />              

    </LinearLayout>     

    <LinearLayout android:orientation="horizontal"
        android:layout_width="290.0dip" android:layout_height="wrap_content"
        android:paddingTop="5dip" android:paddingBottom="5dip" android:layout_marginTop="50dp"
        android:gravity="center_horizontal" >                   

    <ProgressBar 
    android:id="@+id/progressbar"  
    android:max="5" 
    android:progress="0" 
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" 
    style="@android:style/Widget.ProgressBar.Horizontal"
    />      

    <TextView 
        android:id="@+id/tv_counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"/>
    </LinearLayout>     

</LinearLayout>

This is basically the page where I want timer to display just below next button

  • 上图是我想要显示和启动的问题活动 倒计时进度计时器。在测验中共有20个问题。

  • 我想在问题活动开始后立即启动计时器 间隔为2分钟(120秒),并希望它将值减少1 秒不断,直到它    达到0。

  • 此外,我希望进度条每秒减少1% 已过去和进度条旁边显示的值(文本)也应该    同时减少。

  • 与120秒一样,进度条为100%。它应该是99%,持续119秒 等等。

  • 达到0后,应将用户重定向到结果 页(EndgameActivity.java)。

    如果我的代码有任何疑问,我愿意将代码邮寄给任何愿意帮助我的人。

1 个答案:

答案 0 :(得分:-1)

当倒计时停止时,您需要调用设定的问题功能并再次启动计时器。 countdowntimer会有onFinish()函数。您需要调用下一个问题函数。

2问题。与最终倒数计时器相同。您需要在倒数完成后立即关注结果页面