gpt4 book ai didi

android - 在后台通过 setContentView 加载布局时显示对话框

转载 作者:行者123 更新时间:2023-11-29 14:42:45 24 4
gpt4 key购买 nike

我正在使用下面的代码,我想在前面显示对话框并在后台加载内容,但无法做到这一点。请指教。

dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
runOnUiThread(new Runnable(){
public void run() {
setContentView(R.layout.main_layout);
dialog.dismiss();
}
});

最佳答案

我通过阅读下面的链接得到了解决方案并实现了如下代码: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

int glb=0,glbtotal=3;
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};

public void open_next_screen()
{
dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
startLongRunningOperation();
}
private void updateResultsInUi() {

// Back in the UI thread -- update our UI elements based on the data in mResults
switch(glb)
{
case 0:
setContentView(R.layout.main_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

break;
case 1:
part2();

break;
case 2:
part3();

break;
}

}

protected void startLongRunningOperation() {

// Fire off a thread to do some work that we shouldn't do directly in the UI thread
Thread t = new Thread() {
public void run() {


for(glb=0;glb<glbtotal;glb++)
{
mHandler.post(mUpdateResults);

switch(glb)
{
case 0:
part1();
break;

}
}
dialog.dismiss();
}
};
t.start();
}

关于android - 在后台通过 setContentView 加载布局时显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2645271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com