我想在我的屏幕上使用进度条而不是progressDialog。
我在XML-view文件中插入了一个progressBar,我想让它在加载时显示,在不加载时禁用。
所以我使用的是visible,但它发生了,所以剩下的数据就下来了。
我应该如何在异步任务中使用进度条?如何显示和隐藏它?
发布于 2010-11-08 04:44:17
下面是一个最详尽的例子:
public class ScreenSplash extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_splash);
final ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
final TextView textview = (TextView)findViewById(R.id.text);
new MyWorker(this, progress, textview).execute();
}
}
final class MyWorker extends AsyncTask<Void, Integer, Void> {
private static final int titles[] = {R.string.splash_load_timezone,
R.string.splash_load_memory,
R.string.splash_load_genres,
R.string.splash_load_channels,
R.string.splash_load_content};
private static final int progr[] = {30, 15, 20, 25, 20};
private int index;
private final Activity parent;
private final ProgressBar progress;
private final TextView textview;
public MyWorker(final Activity parent, final ProgressBar progress, final TextView textview) {
this.parent = parent;
this.progress = progress;
this.textview = textview;
}
@Override
protected void onPreExecute() {
int max = 0;
for (final int p : progr) {
max += p;
}
progress.setMax(max);
index = 0;
}
@SuppressWarnings("unchecked")
@Override
protected Void doInBackground(final Void... params) {
/* Load timezone. this is very slow - may take up to 3 seconds. */
...
publishProgress();
/* Get more free memory. */
...
publishProgress();
/* Load channels map. */
...
publishProgress();
/* Load genre names. */
...
publishProgress();
/* Preload the 1st screen's content. */
...
publishProgress();
return null;
}
@Override
protected void onProgressUpdate(final Integer... values) {
textview.setText(titles[index]);
progress.incrementProgressBy(progr[index]);
++index;
}
@Override
protected void onPostExecute(final Void result) {
parent.finish();
}
}要显示/隐藏进度条,请使用progress.setVisibility(View.VISIBLE)和progress.setVisibility(View.GONE)。还有View.INVISIBLE常量;它与GONE的不同之处在于没有绘制进度条,但仍然占据了它的空间(对于某些布局来说很有用)。
发布于 2010-11-08 02:55:41
至于在异步任务中使用进度条,您可以使用doInBackground(int Progress)中的PostPrgressUpdate(),并在OnProgressUpdate()方法中相应地更新ProgressBar。至于栏的显示和隐藏,我不能理解您的问题(或问题,请修改)
发布于 2015-10-21 05:10:30
在xml使用中
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>然后在异步任务中
ProgressBar mProgress=(ProgressBar) findViewById(R.id.progress_bar);
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mProgress.setVisibility(View.GONE);
}https://stackoverflow.com/questions/4119009
复制相似问题