gpt4 book ai didi

android - 避免在显示 ProgressDialog 时锁定屏幕

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:26 26 4
gpt4 key购买 nike

我想在后台执行某些指令时显示进度条,但我不想锁定屏幕。此代码正在锁定屏幕:

private class ConvertDataInBackground extends AsyncTask<Params, Void, Boolean> {

private MyProgressDialog mProgressDialog;

@Override
public void onPreExecute() {
if(mProgressDialog == null)
mProgressDialog = MyProgressDialog.show(mActivity, null, null);
}

@Override
protected Boolean doInBackground(Params...params) {
boolean conversionResult = executeConversion(params[0].item1, params[0].item2, params[0].item3);
return conversionResult;
}

@Override
protected void onPostExecute(Boolean result) {
try {
if(mProgressDialog.isShowing())
mProgressDialog.dismiss();
if(result) {
Toast toast = Toast.makeText(mActivity, R.string.conversion_result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

在后台执行指令可能需要一段时间,我希望用户能够使用该界面。你知道我们可以在界面上工作,而 progressDialog 显示在顶部吗?

最佳答案

Android 中的对话框通常是模态的。您可以尝试创建对话框,然后将 FLAG_NOT_TOUCH_MODAL 添加到其窗口参数中 - 但我不确定它是否有效。它背后的想法是这样的:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(...);
dialog.setTitle(...);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

同时请注意,将 ProgressBar 添加到 Activity 的布局并在需要时显示它可能更容易,而不是创建对话框 - 事实上,这正是 Google 文档所推荐的. Android developer's guide on dialogs有这样的话:

Avoid ProgressDialog

Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. However, if you need to indicate loading or indeterminate progress, you should instead follow the design guidelines for Progress & Activity and use a ProgressBar in your layout.

关于android - 避免在显示 ProgressDialog 时锁定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124274/

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