Java 带有倒计时的ProgressBar-Android

Java 带有倒计时的ProgressBar-Android,java,android,progress-bar,countdowntimer,Java,Android,Progress Bar,Countdowntimer,我有一个20秒的倒计时成功地工作在我的琐事游戏。我想在屏幕上添加一个ProgressBar(不是ProgressDialog)。我发现Android开发者指南令人困惑。我在谷歌上搜索了很多例子,并试图将它们合并到我的代码中。现在,当我运行一个游戏时,所有显示的都是一个空条,在游戏的每个问题中都没有“进展” QuestionView.java public class QuestionView extends Activity { int correctAnswers = 0;

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

QuestionView.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);
    }
}
公共类问题视图扩展活动{
int=0;
int=0;
int-answer=0;
int i=0;
长分数=0;
长起始时间=20000;
长间隔=1000;
长点;
布尔TIMERHASSSTARTED=false;
字符串类别;
按钮应答1、应答2、应答3、应答4;
文本视图问题、点计数器、问题编号、计时器;
ArrayList查询;
定时器;
进度条;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);
answer1=(按钮)findViewById(R.id.answer1);
answer2=(按钮)findViewById(R.id.answer2);
answer3=(按钮)findViewById(R.id.answer3);
answer4=(按钮)findViewById(R.id.answer4);
问题=(TextView)findViewById(R.id.question);
类别=getIntent().getStringExtra(“类别”);
querys=getIntent().getParcelableArrayListExtra(“查询”);
pointCounter=(TextView)findViewById(R.id.timer);
问题编号=(TextView)findViewById(R.id.timeElapsedView);
timeCounter=(TextView)findViewById(R.id.timeCounter);
cdTimer=新计时器(开始时间、间隔);
bar=(ProgressBar)findViewById(R.id.ProgressBar);
bar.setUndeterminate(假);
bar.setMax(9);
loadQuestion();
}
公共问题{
进度(一);
如果(i==10){
endquick();
}否则{
如果(!timerHasStarted){
cdTimer.start();
TIMERHASSSTARTED=真;
}否则{
cdTimer.start();
timerHasStarted=错误;
}
answer=querys.get(i).getCorrectAnswer();
question.setText(querys.get(i).getQuery());
answer1.setText(querys.get(i.getA1());
answer2.setText(querys.get(i.getA2());
answer3.setText(querys.get(i.getA3());
answer4.setText(querys.get(i.getA4());
回答1.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
查询.get(i).setSelectedAnswer(0);
如果(答案=0){
正确答案++;
nextQuestion();
}否则{
错误答案++;
nextQuestion();
}
}
});
answer2.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
查询。获取(i)。设置所选答案(1);
如果(答案=1){
正确答案++;
nextQuestion();
}否则{
错误答案++;
nextQuestion();
}
}
});
回答3.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
查询。获取(i)。设置所选答案(2);
如果(答案=2){
正确答案++;
nextQuestion();
}否则{
错误答案++;
nextQuestion();
}
}
});
回答4.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
查询。获取(i)。设置所选答案(3);
如果(答案=3){
正确答案++;
nextQuestion();
}否则{
错误答案++;
nextQuestion();
}
}
});
} 
}
公共ArrayList getquerys(){
返回查询;
}
public void nextQuestion(){
分数=分数+分数;
i++;
loadQuestion();
}
公共类计时器扩展了倒计时{
public void startCountdownTimer(){
}
公共计时器(长启动时间、长间隔){
超级(开始时间、间隔);
}
@凌驾
公共无效onFinish(){
如果(i>=9){
cdTimer.cancel();
endquick();
}否则{
错误答案++;
nextQuestion();
}
}
@凌驾
公共void onTick(长毫秒未完成){
timeCounter.setText(“剩余时间:”+(毫秒直到完成/100));
点数=(毫秒/100)/2;
pointCounter.setText(“剩余点数:+点数”);
如果(i<10){
问题编号.setText(“问题”+(i+1)+“共10个”);
}
}
}
公开考试(){
意向意向=新意向(QuestionView.this,Results.class);
意向。额外(“正确答案”,正确答案);
意图。putExtra(“错误答案”,错误答案);
意图。额外(“分数”,分数);
intent.putParcelableArrayListExtra(“查询”,查询);
意向。额外(“类别”,类别);
星触觉(意向);
}
}
XML代码

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

我要找的是ProgressBar缓慢地向下滴答,直到20秒结束
bar.setProgress((int) Math.round(millisUntilFinished / 1000.0));
bar.setMax(9);
bar.setProgress(i);
bar.setIndeterminate(false);
bar = (ProgressBar)findViewById(R.id.progressbar);
bar.setIndeterminate(false); // May not be necessary
bar.setMax(9);
bar.setProgress(i);