gpt4 book ai didi

android - ProgressDialog 未显示在 Activity 中

转载 作者:太空狗 更新时间:2023-10-29 15:49:21 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中包含一个 ProgressDialog。但它没有出现。

这是我使用 ProgressDialog 的代码 fragment :

public class abcActivity extends Activity {
public boolean onOptionsItemSelected(MenuItem item) {
case XYZ:
ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
callSomeFunction();
dialog.dismiss();
showToast(getString(R.string.SomeString));
break;
}
}

有谁知道为什么对话框没有出现?有什么线索吗?

最佳答案

我认为您的代码在某种意义上是错误的,因为您在 UI 线程中执行所有操作。您必须将 callsomefunction() 放入后台线程。

public void runSomething()
{
showDialog(BACKGROUND_ID);
Thread t = new Thread(new Runnable()
{
public void run()
{
//do something
handler.post(finishThread);
}
});

t.start();
// The progress wheel will only show up once all code coming here has been executed
}

还有

protected Dialog onCreateDialog(int id)
{
if(progressDialog == null) progressDialog = new ProgressDialog(this);
return progressDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
if(id == BACKGROUND_ID)
{
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setMessage("running for long ...");
}
}

Runnable finishThread = new Runnable()
{
public void run()
{
//long running
if(progressDialog != null) progressDialog.dismiss();
}
};

关于android - ProgressDialog 未显示在 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4861710/

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