android–Progressbar与asyncTask共享

android–Progressbar与asyncTask共享,第1张

概述我想在屏幕上使用进度条而不是progressDialog.我在我的XML视图文件中插入了一个progressBar,我希望它在加载时显示,并在加载时禁用它.所以我使用可见,但它发生了,所以其余的数据下降.我应该如何在asynctask中使用progressbar?我该如何显示和隐藏它?解决方法:这是一个最详尽的例子:

我想在屏幕上使用进度条而不是progressDialog.

我在我的XML视图文件中插入了一个progressbar,我希望它在加载时显示,并在加载时禁用它.

所以我使用可见,但它发生了,所以其余的数据下降.

我应该如何在asynctask中使用progressbar?我该如何显示和隐藏它?

解决方法:

这是一个最详尽的例子:

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的区别在于您的进度条未绘制但仍占用其空间(对于某些布局很有用).

总结

以上是内存溢出为你收集整理的android – Progressbar与asyncTask共享全部内容,希望文章能够帮你解决android – Progressbar与asyncTask共享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1103881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存