带进度条的倒数计时器不显示进度
Countdown Timer with Progressbar not show progress
我做了一个带进度条的倒数计时器
但进度条未以百分比显示
像这样
在倒计时中我使用 60000 毫秒(1 分钟)但它现在显示在带有 setprogress 的进度条中
我xml像这样
<ProgressBar
android:id="@+id/progressbartimer"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:min="0"
android:indeterminate="true"
android:visibility="visible" />
在 updatetimer 中,我使用 timelefttxt 只是为了查看倒计时工作与否
public void startstop() {
if(timerunning){
stoptimer();
}else{
startimer();
}
}
public void startimer() {
countDownTimer = new CountDownTimer(timeleftinmilisecond,1000) {
@Override
public void onTick(long millisUntilFinished) {
timeleftinmilisecond = millisUntilFinished;
updatetimer();
}
@Override
public void onFinish() {
Toast.makeText(pretest.this,"jawaban kamu salah",Toast.LENGTH_SHORT).show();
startnum++;
Dialogsalah(pembahasan);
tampiljawaban.add(new tampiljawaban(question,jawaban,"-"));
}
}.start();
timerunning = true;
}
public void updatetimer() {
int progres = (int) ( timeleftinmilisecond/timefull )*100;
progressBar.setProgress(progres);
int minute = (int) timeleftinmilisecond/60000;
int second = (int) timeleftinmilisecond % 60000 / 1000;
String timelefttxt;
timelefttxt = "" + minute;
timelefttxt += ":";
if(second < 10) timelefttxt += "0";
timelefttxt += second;
timertv.setText(timelefttxt);
}
首先,您需要从 XML 中删除 android:indeterminate="true"
使用没有该属性的进度条
<ProgressBar
android:id="@+id/progressbartimer"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:min="0"
android:visibility="visible" />
并像这样在 updateTimer() 中设置进度
public void updatetimer() {
double progress = (60000 - millisUntilFinished) / 1000; // I have hardcoded max timer value i.e 60000 milliseconds i.e 60s
progressBar.setProgress((int) progress);
int minute = (int) timeleftinmilisecond/60000;
int second = (int) timeleftinmilisecond % 60000 / 1000;
String timelefttxt;
timelefttxt = "" + minute;
timelefttxt += ":";
if(second < 10) timelefttxt += "0";
timelefttxt += second;
timertv.setText(timelefttxt);
}
Note : You might need to set the android:max
attribute to 60 if you are using
this code
我做了一个带进度条的倒数计时器 但进度条未以百分比显示
像这样
在倒计时中我使用 60000 毫秒(1 分钟)但它现在显示在带有 setprogress 的进度条中
我xml像这样
<ProgressBar
android:id="@+id/progressbartimer"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:min="0"
android:indeterminate="true"
android:visibility="visible" />
在 updatetimer 中,我使用 timelefttxt 只是为了查看倒计时工作与否
public void startstop() {
if(timerunning){
stoptimer();
}else{
startimer();
}
}
public void startimer() {
countDownTimer = new CountDownTimer(timeleftinmilisecond,1000) {
@Override
public void onTick(long millisUntilFinished) {
timeleftinmilisecond = millisUntilFinished;
updatetimer();
}
@Override
public void onFinish() {
Toast.makeText(pretest.this,"jawaban kamu salah",Toast.LENGTH_SHORT).show();
startnum++;
Dialogsalah(pembahasan);
tampiljawaban.add(new tampiljawaban(question,jawaban,"-"));
}
}.start();
timerunning = true;
}
public void updatetimer() {
int progres = (int) ( timeleftinmilisecond/timefull )*100;
progressBar.setProgress(progres);
int minute = (int) timeleftinmilisecond/60000;
int second = (int) timeleftinmilisecond % 60000 / 1000;
String timelefttxt;
timelefttxt = "" + minute;
timelefttxt += ":";
if(second < 10) timelefttxt += "0";
timelefttxt += second;
timertv.setText(timelefttxt);
}
首先,您需要从 XML 中删除 android:indeterminate="true"
使用没有该属性的进度条
<ProgressBar
android:id="@+id/progressbartimer"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:min="0"
android:visibility="visible" />
并像这样在 updateTimer() 中设置进度
public void updatetimer() {
double progress = (60000 - millisUntilFinished) / 1000; // I have hardcoded max timer value i.e 60000 milliseconds i.e 60s
progressBar.setProgress((int) progress);
int minute = (int) timeleftinmilisecond/60000;
int second = (int) timeleftinmilisecond % 60000 / 1000;
String timelefttxt;
timelefttxt = "" + minute;
timelefttxt += ":";
if(second < 10) timelefttxt += "0";
timelefttxt += second;
timertv.setText(timelefttxt);
}
Note : You might need to set the
android:max
attribute to 60 if you are using this code