有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ProgressBar带有一个倒计时器Android

我有一个20秒的倒计时成功地在我的琐事游戏工作。我想在屏幕上添加一个ProgressBar(不是ProgressDialog)。我发现Android开发者指南令人困惑。我在谷歌上搜索了很多例子,并试图将它们合并到我的代码中。现在,当我运行一个游戏时,显示的只是一个空条,在游戏的每个问题中都没有“进展”

问题视图。java

public class QuestionView extends Activity  {

    int correctAnswers = 0;
    int wrongAnswers = 0;
    int answer = 0;
    int i = 0;

    long score = 0;

    long startTime = 20000;
    long interval = 1000;
    long points;

    boolean timerHasStarted = false;

    String category;

    Button answer1, answer2, answer3, answer4;
    TextView question, pointCounter, questionNumber, timeCounter;

    ArrayList<Question> queries;
    Timer cdTimer;

    ProgressBar bar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionviewmain);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);

        category = getIntent().getStringExtra("category");
        queries = getIntent().getParcelableArrayListExtra("queries");

        pointCounter = (TextView)findViewById(R.id.timer);
        questionNumber = (TextView)findViewById(R.id.timeElapsedView);
        timeCounter = (TextView)findViewById(R.id.timeCounter);

        cdTimer = new Timer(startTime, interval);

        bar = (ProgressBar)findViewById(R.id.progressbar);
        bar.setIndeterminate(false);
        bar.setMax(9);

        loadQuestion();
    }

    public void loadQuestion() {

            bar.setProgress(i);

        if(i == 10) {

            endQuiz();

        } else {

            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.start();
                timerHasStarted = false;
            }

            answer = queries.get(i).getCorrectAnswer();

            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(1);
                    if(answer == 1) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(2);
                    if(answer == 2) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(3);
                    if(answer == 3) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });
        } 
    }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public void nextQuestion() {
        score = score + points;
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {

        public void startCountdownTimer() {  
        }

        public Timer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            if(i >= 9) {
                cdTimer.cancel();
                endQuiz();
            } else {
                wrongAnswers++;
                nextQuestion();
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            timeCounter.setText("Time remaining: " + (millisUntilFinished / 100));
            points = (millisUntilFinished / 100) / 2;
            pointCounter.setText("Points remaining: " + points);
            if(i < 10) {
                questionNumber.setText("Question " + (i + 1) + " of 10");
            }
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putExtra("score", score);
        intent.putParcelableArrayListExtra("queries", queries);
        intent.putExtra("category", category);
        startActivity(intent);
    }
}

XML代码

<ProgressBar 
                安卓:id="@+id/progressbar"
                安卓:layout_height="wrap_content" 
                安卓:layout_width="wrap_content"
                安卓:visibility="visible" 
                style="@安卓:style/Widget.ProgressBar.Horizontal" />

共 (1) 个答案

  1. # 1 楼答案

    What I am looking for is for the ProgressBar to slowly tick down until the 20 seconds the user is allotted for each question is gone.

    新答案
    为此,让我们在^{中添加一行新词:

    bar.setProgress((int) Math.round(millisUntilFinished / 1000.0));
    

    (你可能需要调整数据类型,我远离我的编译器……而且我不喜欢倒计时类,它不准确,经常跳过倒数第二个数字。我在这里写了一个替代类:android CountDownTimer - additional milliseconds delay between ticks


    原始答案
    我有几点建议:

    • 您是否为ProgressBar定义了最大值

      bar.setMax(9);
      
    • 我建议加载i作为进度,而不是常量值10:

      bar.setProgress(i);
      
    • 如果您仍然没有看到任何进展,请确保您没有处于不确定模式:

      bar.setIndeterminate(false);
      

      (假设您使用的ProgressBar可以depict progress。)


    添加
    将此代码移到onCreate()

    bar = (ProgressBar)findViewById(R.id.progressbar);
    bar.setIndeterminate(false); // May not be necessary
    bar.setMax(9);
    

    然后将这一行移动到^{

    bar.setProgress(i);
    

    否则,进程将永远不会更新,因为您只创建了一个倒计时程序,而且您从未实际调用startCountdownTimer()