用countdowntimer进度 - Android电子 [英] ProgressBar with a countdowntimer - Android

查看:119
本文介绍了用countdowntimer进度 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个20秒倒计时顺利工作在我的问答游戏。我想添加一个进度(不是 ProgressDialog )到屏幕上。我发现为Android混乱的开发者指南。我GOOGLE了大量实例,并试图将它们组合成我的code。现在所有的,当我运行游戏与游戏中的每个问题时没有取得进步的空栏显示。

QuestionView.java

 公共类QuestionView延伸活动{    INT correctAnswers = 0;
    INT wrongAnswers = 0;
    INT答案= 0;
    INT I = 0;    长得分= 0;    长STARTTIME = 20000;
    长间隔= 1000;
    长点;    布尔timerHasStarted = FALSE;    串类;    按钮ANSWER1,ANSWER2,ANSWER3,answer4;
    TextView的问题,pointCounter,questionNumber,timeCounter;    ArrayList的<问题>查询;
    定时器cdTimer;    进度条;    @覆盖
    公共无效的onCreate(捆绑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(类别)。
        查询= getIntent()getParcelableArrayListExtra(查询)。        pointCounter =(的TextView)findViewById(R.id.timer);
        questionNumber =(的TextView)findViewById(R.id.timeElapsedView);
        timeCounter =(的TextView)findViewById(R.id.timeCounter);        cdTimer =新的Timer(startTime时,间隔);        巴=(进度)findViewById(R.id.progressbar);
        bar.setIndeterminate(假);
        bar.setMax(9);        loadQuestion();
    }    公共无效loadQuestion(){            bar.setProgress(ⅰ);        如果(我== 10){            endQuiz();        }其他{            如果(!timerHasStarted){
                cdTimer.start();
                timerHasStarted = TRUE;
            }其他{
                cdTimer.start();
                timerHasStarted = FALSE;
            }            答案= queries.get(I).getCorrectAnswer();            question.setText(queries.get(ⅰ).getQuery());            answer1.setText(queries.get(ⅰ).getA1());
            answer2.setText(queries.get(ⅰ).getA2());
            answer3.setText(queries.get(ⅰ).getA3());
            answer4.setText(queries.get(ⅰ).getA4());            answer1.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    queries.get(ⅰ).setSelectedAnswer(0);
                    如果(答案== 0){
                        correctAnswers ++;
                        下一个问题();
                    }其他{
                        wrongAnswers ++;
                        下一个问题();
                    }
                }
            });            answer2.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    queries.get(ⅰ).setSelectedAnswer(1);
                    如果(回答== 1){
                        correctAnswers ++;
                        下一个问题();
                    }其他{
                        wrongAnswers ++;
                        下一个问题();
                    }
                }
            });            answer3.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    queries.get(ⅰ).setSelectedAnswer(2);
                    如果(回答== 2){
                        correctAnswers ++;
                        下一个问题();
                    }其他{
                        wrongAnswers ++;
                        下一个问题();
                    }
                }
            });            answer4.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    queries.get(ⅰ).setSelectedAnswer(3);
                    如果(回答== 3){
                        correctAnswers ++;
                        下一个问题();
                    }其他{
                        wrongAnswers ++;
                        下一个问题();
                    }
                }
            });
        }
    }    公众的ArrayList<问题> getQueries(){
        返回查询;
    }    公共无效nextQuestion(){
        得分=评分+分;
        我++;
        loadQuestion();
    }    公共类定时器扩展CountDownTimer {        公共无效startCountdownTimer(){
        }        公共定时器(长的startTime,长间隔){
            超(startTime时,间隔);
        }        @覆盖
        公共无效onFinish(){
            如果(ⅰ> = 9){
                cdTimer.cancel();
                endQuiz();
            }其他{
                wrongAnswers ++;
                下一个问题();
            }
        }        @覆盖
        公共无效onTick(长millisUntilFinished){
            timeCounter.setText(剩余时间+(millisUntilFinished / 100));
            点=(millisUntilFinished / 100)/ 2;
            pointCounter.setText(积分剩余:+分);
            如果(ⅰ小于10){
                questionNumber.setText(问题+第(i + 1)+10);
            }
        }
    }    公共无效endQuiz(){
        意向意图=新意图(QuestionView.this,Results.class);
        intent.putExtra(correctAnswers​​,correctAnswers);
        intent.putExtra(wrongAnswers​​,wrongAnswers);
        intent.putExtra(得分,得分);
        intent.putParcelableArrayListExtra(查询,查询);
        intent.putExtra(类别,类别);
        startActivity(意向);
    }
}

XML code

 <进度
                机器人:ID =@ + ID /进度
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_width =WRAP_CONTENT
                机器人:知名度=看得见
                风格=@安卓风格/ Widget.ProgressBar.Horizo​​ntal/>


解决方案

  

我所寻找的是在进度条慢慢剔下来,直到20秒每题不见了用户分配。


新建答案结果
要做到这一点,让我们一个新的行添加到 onTick()

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

(您可能需要调整的数据类型,我是从我的编译器了......此外,我不喜欢CountDownTimer类,它是不准确的,往往跳过倒数第二个号码。我写到这里的替代类:机器人CountDownTimer - 额外的延迟毫秒之间蜱


原来的答案结果
我有几个指针:


  • 你有没有为你的进度规定的最大值?

      bar.setMax(9);


  • 我建议加载 I 的进步,而不是10的恒定值:

      bar.setProgress(I)


  • 如果您还没有看到任何进展确保你是不是在不确定模式:

      bar.setIndeterminate(假);

    (这个假设你正在使用进度,能够<一个href=\"https://developer.android.com/reference/android/widget/ProgressBar.html#setIndeterminate%28boolean%29\"相对=nofollow>描绘进步。)



加入结果
移动此code到的onCreate()

 巴=(进度)findViewById(R.id.progressbar);
bar.setIndeterminate(假); //可能没有必要
bar.setMax(9);

然后移动此行 loadQuestion()

  bar.setProgress(I)

否则进展将永远不会被更新,因为你只创建一个CountDownTimer,你永远不实际调用 startCountdownTimer()

I have a 20 second countdown timer successfully working on my trivia game. I want to add a ProgressBar (not a ProgressDialog) to the screen. I found the developer guide for Android confusing. I googled lots of examples and tried to combine them into my code. Right now all that displays when I run a game is an empty bar with no "progress" made during each question of the game.

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

XML code

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

解决方案

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.

New Answer
To do this let's add a new line to onTick():

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

(You may need to tweak the data types, I am away from my compiler... Also I don't like the CountDownTimer class, it is inaccurate and often skips the second to last number. I wrote an alternate class here: android CountDownTimer - additional milliseconds delay between ticks)


Original Answer
I have a couple pointers:

  • Have you defined a maximum value for your ProgressBar?

    bar.setMax(9);
    

  • I suggest loading i as the progress instead of the constant value of 10:

    bar.setProgress(i);
    

  • If you still do not see any progress ensure that you are not in indeterminate mode:

    bar.setIndeterminate(false);
    

    (This assume that you are using a ProgressBar that can depict progress.)


Addition
Move this code into onCreate():

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

Then move this line to loadQuestion():

bar.setProgress(i);

Otherwise the progress will never be updated since you only create one CountDownTimer and you never actually call startCountdownTimer().

这篇关于用countdowntimer进度 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆