没有 AsyncTask 的 ProgressBar
ProgressBar without AsyncTask
我想显示进度条,生成计算,然后在我这样做之后我希望进度条消失,我想将 textview 设置为生成结果的结果。然而,当我执行以下操作时,进度条永远不会显示,但 textview 会适当更新。我最初想使用 AsyncTask 来执行此操作,但由于不推荐使用它,我想尝试一种现代方法,但恐怕我无法让它工作。谁能看出哪里出了问题?
genResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
// do onPreExecute stuff
pb.setVisibility(View.VISIBLE);
}
});
// do your stuff
boolean res = exp.genResult();
long startTime = System.nanoTime();
while(System.nanoTime()-startTime<50000){
}
result.outcomes.add(res);
runOnUiThread(new Runnable() {
public void run() {
// do onPostExecute stuff
pb.setVisibility(View.INVISIBLE);
lastRes.setText(Boolean.toString(res));
}
});
}
}).start();
}
});
下面是 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/gen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="generate result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941" />
<TextView
android:id="@+id/title_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
app:layout_constraintBottom_toTopOf="@+id/gen_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />
<Button
android:id="@+id/detail_bin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Details"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.941"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.005" />
<Button
android:id="@+id/backbutton_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="go back"
app:layout_constraintEnd_toStartOf="@+id/detail_bin_button"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lastresultbin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:text="lastresult"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/backbutton_bin"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="@+id/probabilityViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="Probability"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toEndOf="@+id/lastresultbin"
app:layout_constraintTop_toBottomOf="@+id/detail_bin_button" />
<TextView
android:id="@+id/plaintext_lastres_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Last Result"
app:layout_constraintBottom_toTopOf="@+id/lastresultbin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/backbutton_bin"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/plaintext_prob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="164dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="Probability of Success"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detail_bin_button" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginTop="148dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是 while
循环中使用的 System.nanoTime()
太快无法完成。当我们切换到使用 System.currentTimeInMillis()
时,我们会注意到延迟有所不同,因此微调器会按预期显示和隐藏。
我想显示进度条,生成计算,然后在我这样做之后我希望进度条消失,我想将 textview 设置为生成结果的结果。然而,当我执行以下操作时,进度条永远不会显示,但 textview 会适当更新。我最初想使用 AsyncTask 来执行此操作,但由于不推荐使用它,我想尝试一种现代方法,但恐怕我无法让它工作。谁能看出哪里出了问题?
genResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
// do onPreExecute stuff
pb.setVisibility(View.VISIBLE);
}
});
// do your stuff
boolean res = exp.genResult();
long startTime = System.nanoTime();
while(System.nanoTime()-startTime<50000){
}
result.outcomes.add(res);
runOnUiThread(new Runnable() {
public void run() {
// do onPostExecute stuff
pb.setVisibility(View.INVISIBLE);
lastRes.setText(Boolean.toString(res));
}
});
}
}).start();
}
});
下面是 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/gen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="generate result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941" />
<TextView
android:id="@+id/title_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
app:layout_constraintBottom_toTopOf="@+id/gen_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />
<Button
android:id="@+id/detail_bin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Details"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.941"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.005" />
<Button
android:id="@+id/backbutton_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="go back"
app:layout_constraintEnd_toStartOf="@+id/detail_bin_button"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lastresultbin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:text="lastresult"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/backbutton_bin"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="@+id/probabilityViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="Probability"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toEndOf="@+id/lastresultbin"
app:layout_constraintTop_toBottomOf="@+id/detail_bin_button" />
<TextView
android:id="@+id/plaintext_lastres_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Last Result"
app:layout_constraintBottom_toTopOf="@+id/lastresultbin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/backbutton_bin"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/plaintext_prob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="164dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="Probability of Success"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detail_bin_button" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginTop="148dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是 while
循环中使用的 System.nanoTime()
太快无法完成。当我们切换到使用 System.currentTimeInMillis()
时,我们会注意到延迟有所不同,因此微调器会按预期显示和隐藏。