Android:带有 ProgressBar 和 setcurrentTab 的线程
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AsyncTask 获取
一些提示:
use AsyncTask for this
some hints:
问题是,您正在启动一个不会影响您的主 UI 的线程。所以最终发生的情况是,您的线程将单独运行,现在将允许执行代码的下一行。因此,在您的情况下,
无论您的线程也同时执行,这些行都将被执行。因此,您在这里可以做的是,您可以选择 AsyncTask 或创建处理程序来处理这部分代码。你必须像这样改变你的代码。
在您的 onCreate() 中执行此操作
,现在在您的线程中,像这样调用处理程序,
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,
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()
And now in your thread,call the handler like this,