我有大约3分钟的计时器,并且我添加了一个progress bar
,在每个1860 milliseconds.
之后添加了1点进度条
因此,在1860000
毫秒后,它完成了进度。
问题在于它会在开始时填充进度条,而不显示加载动画。
这是我的代码。
countDownTimer=new CountDownTimer(186000, 1860) {
public void onTick(long millisUntilFinished) {
progressBar.setProgress(time+=1);
Log.d("seconds remaining: ", String.valueOf(time));
}
public void onFinish() {
finish();
time=0;
}
}.start();
}
布局文件
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="4dp"
android:layout_marginTop="30dp"
android:max="100"
android:progressDrawable="@drawable/progressbar_background"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toBottomOf="@+id/imageView19" />
如果我删除progressDialog.setProgress()
方法,它会显示空白的进度条,并且如果我添加此行,则progress bar
会在开始时自动填充。
答案 0 :(得分:0)
您应该使用以下方式设置进度栏:
//prints out all the disarium numbers lower than a user defined number
static void Main(string[] args)
{
//assigns the variables
string number , strI;
double calculation;
//asks the user for string number
Console.WriteLine("Give a number between 10 and 1.000.000. This program will tell you all the disarium numbers lower than the given number.");
number = Console.ReadLine();
Console.WriteLine("");
//calcules all the disarium numbers lower than string number
for (Int64 I = 10;I < Convert.ToInt64(number);I++)
{
calculation = 0;
strI = Convert.ToString(I);
for (int i = 0; i < strI.Length; i++)
{
calculation += Math.Pow(Convert.ToDouble(strI[i].ToString()), Convert.ToDouble(i) + 1.0);
}
//Prints out all the desarium numbers below string number
if (Convert.ToInt64(calculation) == I)
{
Console.WriteLine(I + " is a disarium number.");
}
}
}
然后onTick(),致电:
<ProgressBar
...
android:max="1860000"
/>
答案 1 :(得分:0)
我认为问题在于您使用的变量time
在匿名类CountDownTimer
的范围之外声明和初始化。您可以通过在调试模式下运行CountDownTimer或在调用time
之前简单地执行System.out.printnln(time)
来检查CountDownTimer中的setProgress
是否具有正确的值。
要确保您在进度栏中设置的值是正确的,只需执行以下操作:
progressBar.setProgress(100 - millisUntilFinished *100 /186000)
答案 2 :(得分:-1)
问题在于您正在主线程上执行任务,因此视图被冻结,直到任务完成为止。我建议您为此目的使用异步任务,可以尝试如下操作:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
LongTask timer = new LongTask();
timer.execute();
}
private class LongTask extends AsyncTask<Void, Integer, Void> {
private static final int INTERVAL = 1860;
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i <= 100; i++) {
publishProgress(i);
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
Thread.interrupted();
}
Log.d("Milliseconds elapsed: %s", String.valueOf(i * INTERVAL));
}
return null;
}
@Override
protected void onPostExecute(Void result) {}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Integer... values) {
int progress = values[0];
progressBar.setProgress(progress);
}
}
}