Android:带有 ProgressBar 和 setcurrentTab 的线程

发布于 2024-12-24 02:21:14 字数 857 浏览 1 评论 0原文

   final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                        "Loading. Please wait...", true);
                Thread ProgressThread = new Thread() {
                    @Override
                    public void run() {
                        try {
                                     sleep(3000);   
                                     Pdialog.dismiss();

                        } catch(InterruptedException e) {
                            // do nothing
                        } finally {

                        }
                    }
                };
                ProgressThread.start();
                TabHost1 TabHost1Object2 = new TabHost1();
                TabHost1Object2.tabHost.setCurrentTab(2);

我对这个线程的问题是它在进度对话框启动之前设置当前选项卡。我做错了什么? 我希望对话框运行并关闭,并在线程完成后设置选项卡。

   final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                        "Loading. Please wait...", true);
                Thread ProgressThread = new Thread() {
                    @Override
                    public void run() {
                        try {
                                     sleep(3000);   
                                     Pdialog.dismiss();

                        } catch(InterruptedException e) {
                            // do nothing
                        } finally {

                        }
                    }
                };
                ProgressThread.start();
                TabHost1 TabHost1Object2 = new TabHost1();
                TabHost1Object2.tabHost.setCurrentTab(2);

The problem I have with this thread is that it sets the current tab before the progress dialog starts. What have i done wrong ?
I want the dialog to run and dismiss, and after thread is done set tab.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-12-31 02:21:14

使用 AsyncTask 获取

一些提示:

public class BackgroundAsyncTask extends  AsyncTask<Void, Integer, Void> {  
    int myProgress;
    @Override
    protected void onPostExecute(Void result) {
    TabHost1 tab = new TabHost1();
    tab.tabHost.setCurrentTab(2);
    progressBar.dismiss();

    }
    @Override
    protected Void doInBackground(Void... params) {   
        while(myProgress<100){
            myProgress++;
            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer p) {   
        progressBar.setProgress(p);
    }

}

use AsyncTask for this

some hints:

public class BackgroundAsyncTask extends  AsyncTask<Void, Integer, Void> {  
    int myProgress;
    @Override
    protected void onPostExecute(Void result) {
    TabHost1 tab = new TabHost1();
    tab.tabHost.setCurrentTab(2);
    progressBar.dismiss();

    }
    @Override
    protected Void doInBackground(Void... params) {   
        while(myProgress<100){
            myProgress++;
            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer p) {   
        progressBar.setProgress(p);
    }

}
一世旳自豪 2024-12-31 02:21:14

问题是,您正在启动一个不会影响您的主 UI 的线程。所以最终发生的情况是,您的线程将单独运行,现在将允许执行代码的下一行。因此,在您的情况下,

            TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

无论您的线程也同时执行,这些行都将被执行。因此,您在这里可以做的是,您可以选择 AsyncTask 或创建处理程序来处理这部分代码。你必须像这样改变你的代码。

在您的 onCreate() 中执行此操作

           Handler handler;
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
         Pdialog.dismiss();     
             TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

         }
       };

,现在在您的线程中,像这样调用处理程序,

  final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                    "Loading. Please wait...", true);
            Thread ProgressThread = new Thread() {
                @Override
                public void run() {
                    try {
                                 sleep(3000);   


                    } catch(InterruptedException e) {
                        // do nothing
                    } finally {

                             handler.sendEmptyMessage(0);
                    }
                }
            };
this will allow your tabhost to wait until the thread gets executed and will come into view after thread finishes execution. 

The thing is that,you are starting a thread which will not affect your main UI. So what eventually happens is that, your thread will run separately which will now allow the next lines of your code to be executed. So in your case,

            TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

these lines will be executed irrespective to your thread which is also getting executed simultaneously. So what you can do here is you can either go for AsyncTask or create handlers to handle this part of your code. You have to change your code like this.

Do this in your onCreate()

           Handler handler;
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
         Pdialog.dismiss();     
             TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

         }
       };

And now in your thread,call the handler like this,

  final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                    "Loading. Please wait...", true);
            Thread ProgressThread = new Thread() {
                @Override
                public void run() {
                    try {
                                 sleep(3000);   


                    } catch(InterruptedException e) {
                        // do nothing
                    } finally {

                             handler.sendEmptyMessage(0);
                    }
                }
            };
this will allow your tabhost to wait until the thread gets executed and will come into view after thread finishes execution. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文