Подтвердить что ты не робот

Обратный отсчет Android ProgressBar

Я делаю викторину для Android, и я хочу ограниченное время, чтобы ответить на каждый вопрос. Поэтому я хочу отобразить ProgressBar под ответами, которые отсчитываются от, например, от 5 до 0 (секунд). И когда он достигает нуля, я хочу кое-что сделать. У меня опрос и все работает, я просто хочу добавить ProgressBar.

Спасибо заранее!

4b9b3361

Ответ 1

вы можете использовать таймер обратного отсчета в android.

Вот один пример, на который вы можете обратиться Нажмите здесь

вы можете использовать ниже ProgressBar в своей деятельности.

   <ProgressBar 
    android:id="@+id/progressbar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:max="100"
    android:progress="0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/bottom_header_relativelayout"
    />

Используйте CountDownTimer Как ниже код в своей деятельности.

ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
int i=0;

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++;
            mProgressBar.setProgress((int)i*100/(5000/1000));

        }

        @Override
        public void onFinish() {
        //Do what you want 
            i++;
            mProgressBar.setProgress(100);
        }
    };
    mCountDownTimer.start();

Ответ 2

Вы можете использовать ObjectAnimator для анимации хода ProgressBar:

ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", 0, 100);
animation.setDuration(5000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) { }

    @Override
    public void onAnimationEnd(Animator animator) {
        //do something when the countdown is complete
    }

    @Override
    public void onAnimationCancel(Animator animator) { }

    @Override
    public void onAnimationRepeat(Animator animator) { }
});
animation.start();

Ответ 3

Не требуется декларация XML

ProgressDialog TempDialog;
CountDownTimer CDT;
int i =5;

TempDialog = new ProgressDialog(Your_Class_Name.this);
TempDialog.setMessage("Please wait...");
TempDialog.setCancelable(false);
TempDialog.setProgress(i);
TempDialog.show();

CDT = new CountDownTimer(5000, 1000)
{
    public void onTick(long millisUntilFinished)
    {
        TempDialog.setMessage("Please wait.." + i + " sec");
        i--;
    }

    public void onFinish()
    {
        TempDialog.dismiss();
        //Your Code ...
    }
}.start();