Android ProgressBar 倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10241633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ProgressBar countdown
提问by simtaxman
I'm making a quiz for Android and I want a limited time to answer every question. So I want to display a ProgressBar under the answers that counts down from, for example, 5 to 0 (seconds). And when it reaches zero I want to do some stuff. I have the quiz and everything working, I just want to add the ProgressBar.
我正在为 Android 做一个测验,我想在有限的时间内回答每一个问题。所以我想在答案下显示一个 ProgressBar,例如从 5 到 0(秒)倒计时。当它达到零时,我想做一些事情。我有测验并且一切正常,我只想添加 ProgressBar。
Thanks in advance!
提前致谢!
回答by Herry
you can use countdown timer in android .
您可以在 android 中使用倒数计时器。
Here is one Example you can Refer Click Here
这是一个示例,您可以参考单击此处
you can use below ProgressBar in your Activity.
您可以在您的活动中使用下面的 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"
/>
Use CountDownTimer
Like Below code in your Activity.
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();
回答by adamdport
You could use an ObjectAnimator
to animate the progress of the ProgressBar
:
您可以使用 anObjectAnimator
为 的进度设置动画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();
回答by msl
No need for XML declaration
无需 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();