如何重启倒计时器进度条动画?

时间:2016-11-28 08:16:19

标签: android progress-bar countdowntimer

大家好日子:)我需要帮助我的测验应用程序,我是新的android,我的问题是我怎么能重新启动进度条的动画?每次我得到正确的答案。计时器已经用正确的答案重新启动每个问题,但进度条的动画仍在运行。我已经研究过如何重启进度条。所有答案都非常感谢tnx和advance :)每个问题有30秒倒计时

这是我的进度计时器代码

public class QuestionActivity extends Activity {

MediaPlayer mySound;
List<Question> quesList;
int score = 0;
int qid = 0;
int i = 5;


ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;

// Animation
Animation animFadein;


Question currentQ;
TextView txtQuestion, scored;
Button button1, button2, button3, button4,helpbtn;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;


public void help (View view){

    helpbtn=(Button)findViewById(R.id.helpbtn);
    helpbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String AnswerString = currentQ.getANSWER();

            //match DB answer to selected answer, turn it visible if it is correct
            if(button1.getText().equals(AnswerString)){
                button1.setBackgroundColor(Color.GRAY);
            } else if(button2.getText().equals(AnswerString)){
                button2.setBackgroundColor(Color.GRAY);
            } else if(button3.getText().equals(AnswerString)){
                button3.setBackgroundColor(Color.GRAY);
            } else if(button4.getText().equals(AnswerString)){
                button4.setBackgroundColor(Color.GRAY);
            }

            //eliminate 2 button
            // button1 .setVisibility(View.INVISIBLE);
            // button2 .setVisibility(View.INVISIBLE);
            // button3 .setVisibility(View.INVISIBLE);
            // button4 .setVisibility(View.INVISIBLE);

        }
    });
}
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qestion);
    QuizHelper db = new QuizHelper(this);  // my question bank class
    quesList = db.getAllQuestions();  // this will fetch all questions
    currentQ = quesList.get(qid); // the current question
    mySound = MediaPlayer.create(this, R.raw.bensoundcute); // music background
    mySound.start();
    mySound.setLooping(true);

    txtQuestion = (TextView) findViewById(R.id.txtQuestion);
    // load the textQuestion animation
    animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.fade_in);
    // the text view in which the question will be displayed
    // the 4 buttons,
    // the idea is to set the text of 4 buttons with the options from question bank
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);

    // the text view in which score will be displayed
    scored = (TextView) findViewById(R.id.score);
    // method which will set the things up for our game

    setQuestionView(false);

    mProgressBar = (ProgressBar) findViewById(progressbar);
    mProgressBar.setProgress(i);
    mProgressBar.setMax(100);
    mProgressBar.setIndeterminate(false);

    mCountDownTimer = new CountDownTimer(30000,299) {
            @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onTick(long millisUntilFinished) {
                Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
                i++;
                mProgressBar.setRotation(180);
                mProgressBar.setProgress(i);
            }
            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "TIME is UP!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(QuestionActivity.this,
                        ResultActivity.class);
                // passing the int value
                Bundle b = new Bundle();
                b.putInt("score", score); // Your score
                intent.putExtras(b); // Put your score to your next
                startActivity(intent);
                mCountDownTimer.cancel();
                finish();
            }
    };
        txtQuestion.setAnimation(animFadein);
        txtQuestion.startAnimation(animFadein);
        mCountDownTimer.start();
    // button click listeners
    final MediaPlayer buttonSound = MediaPlayer.create(this, R.raw.play);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            // passing the button text to other method
            // to check whether the answer is correct or not
            // same for all three buttons
            getAnswer(button1.getText().toString());

        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            getAnswer(button2.getText().toString());

        }
    });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();

            getAnswer(button3.getText().toString());

        }
    });
    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            getAnswer(button4.getText().toString());

        }
    });
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onPause(){
    super.onPause();
    mySound.release();
    finish();
}
@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setMessage("Do you want to Exit?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user pressed "yes", then he is allowed to exit from application
            dialog.dismiss();
            onYesClick();
        }
        private void onYesClick() {
            Intent setIntent = new Intent(Intent.ACTION_MAIN);
            setIntent.addCategory(Intent.CATEGORY_HOME);
            setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(setIntent);
            mCountDownTimer.cancel();
            finish();
            QuestionActivity.this.finish();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user select "No", just cancel this dialog and continue with app
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
public void getAnswer(String AnswerString) {
    //If timer is running stop and restart
    if(mCountDownTimer != null) {
        mCountDownTimer.cancel(); // Stop Timer
        mCountDownTimer.start(); // Start timer
    }
    if (currentQ.getANSWER().equals(AnswerString)) {
         // if conditions matches increase the int (score) by 1
         // and set the text of the score view
        //Intent intent = new Intent(this, QuestionActivity.class);
        //startActivity(intent);
        Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
        score++;
        scored.setText("Score:  " + score + " /100");
        txtQuestion.setAnimation(animFadein);
        txtQuestion.startAnimation(animFadein);
        mProgressBar.setProgress(0);
        mCountDownTimer.cancel();

    }
    else {
        // if unlucky start activity and finish the game
        Toast.makeText(getApplicationContext(),
                "Sorry! Better luck next time.",
                Toast.LENGTH_SHORT)
                .show();
        Intent intent = new Intent(QuestionActivity.this,
                ResultActivity.class);
        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        setQuestionView(false);
        startActivity(intent);
        mCountDownTimer.cancel();
        finish();
    }
    if(qid < 100) {
        // if questions are not over then do this
        currentQ = quesList.get(qid);
        setQuestionView(true);
        txtQuestion.setAnimation(animFadein);
        txtQuestion.startAnimation(animFadein);

         button1 .setBackgroundColor(Color.WHITE);
         button2 .setBackgroundColor(Color.WHITE);
         button3 .setBackgroundColor(Color.WHITE);
         button4 .setBackgroundColor(Color.WHITE);
    }
    else {
        // if unlucky start activity and finish the game
        Toast.makeText(getApplicationContext(),
                "Game Over.",
                Toast.LENGTH_SHORT)
                .show();
        Intent intent = new Intent(QuestionActivity.this,
                ResultActivity.class);
        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        mCountDownTimer.cancel();
        finish();
    }
}
private boolean setQuestionView(boolean b) {
    // the method which will put all things together

    txtQuestion.setText(currentQ.getQUESTION());
    button1.setText(currentQ.getOPTA());
    button2.setText(currentQ.getOPTB());
    button3.setText(currentQ.getOPTC());
    button4.setText(currentQ.getOPTD());

    qid++;
    return b;
}

2 个答案:

答案 0 :(得分:0)

如果答案正确,请将进度条度设置为零。 如果答案是正确的,请应用此行。

mProgressBar.setRotation(0);

答案 1 :(得分:0)

看起来您的意思是要重置进度。在这种情况下,请致电mProgressBar.setProgress(0)。另外,请不要忘记通过调用mCountDownTimer.cancel()来取消计时器。