1

实施 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;
    }
    
}

问题.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>

这基本上是我希望计时器显示在下一个按钮下方的页面

  • 上图是我的问题活动,我想显示并启动倒数进度计时器。测验共有 20 个问题。

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

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

  • 就像 120 秒,进度条是 100% 。它应该是 99% 持续 119 秒,依此类推。

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

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

4

1 回答 1

-1

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

2 问题。与最终倒计时相同。倒计时结束后,您需要立即进入结果页面

于 2013-06-28T12:53:21.173 回答