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);
我对此主题的问题是它在进度对话框启动之前设置了当前选项卡。我做错了什么? 我希望对话框运行和解除,并在线程完成后设置选项卡。
答案 0 :(得分:2)
使用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);
}
}
答案 1 :(得分:0)
问题是,您正在启动一个不会影响主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.