我想在屏幕上使用进度条而不是progressDialog.
我在我的XML视图文件中插入了一个progressBar,我希望它在加载时显示,并在加载时禁用它.
所以我使用可见,但它发生了,所以其余的数据下降.
我应该如何在asynctask中使用progressbar?我该如何显示和隐藏它?
JBM*_*JBM 26
这是一个最详尽的例子:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
显示/隐藏您的进度条使用progress.setVisibility(View.VISIBLE)和progress.setVisibility(View.GONE).也有View.INVISIBLE不变; 与之不同的GONE是,您的进度条未绘制但仍占用其空间(对于某些布局非常有用).
至于使用异步任务中的进度栏,您可以使用PostPrgressUpdate()您的方法doInBackground(int Progress)并OnProgressUpdate()相应地更新进度栏。至于栏的显示和隐藏我无法理解你的问题(或者问题请修改)
| 归档时间: |
|
| 查看次数: |
34313 次 |
| 最近记录: |