我正在制作一款Android的问答游戏,希望设置每个问题的答题时间限制。因此我想在答案下方显示一个进度条,从5秒到0秒倒计时。当时间达到0秒时,我想执行一些操作。我已经完成了整个问答游戏的开发,只是想添加这个进度条。
谢谢提前!
我正在制作一款Android的问答游戏,希望设置每个问题的答题时间限制。因此我想在答案下方显示一个进度条,从5秒到0秒倒计时。当时间达到0秒时,我想执行一些操作。我已经完成了整个问答游戏的开发,只是想添加这个进度条。
谢谢提前!
你可以在Android中使用倒计时器。
这里有一个例子供你参考:点击这里
你可以在你的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"
/>
在您的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();
您可以使用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();
No need for XML declaration
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();
val maxSecond = 60L
val timerBar = viewForm.findViewById<ProgressBar>(R.id.pb_timer)
timerBar.max = 100
timerBar.progress = 100
var distinct = 0.0f
object : CountDownTimer(1000 * maxSecond, 1000) {
override fun onTick(millisUntilFinished: Long) {
val second = (millisUntilFinished / 1000.0f).roundToInt()
val unit = (second.toFloat() / 100f)
if(distinct == 0f){
distinct = 100f / unit
}
val cal = unit * distinct
timerBar.progress = cal.toInt()
}
override fun onFinish() {
Log.d("check", "done!")
}
}.start()